2010-06-22 74 views
0

alt text http://img24.imageshack.us/img24/3365/sqlnuts.jpgSQL瘋了!不接受參數,?

以及繼承人的代碼


public void select_table_names() 
     {//start

/* display all tables*/ string commandString = null; SqlConnection conn = null; SqlCommand command = null; SqlDataReader reader = null; ArrayList list = new ArrayList(); try { // commandString = "SELECT TABLE_SCHEMA FROM INFORMATION_SCHEMA.TABLES ";// @table "; //string columns; //string tables; //columns = "TABLE_SCHEMA"; //tables = "INFORMATION_SCHEMA.TABLES"; commandString = "SELECT @pthis FROM @tables"; //note when the @tables is replaced by info..schema, still the result is like in figure two" //commandString = "SELECT "+columns+" FROM "+tables; conn = new SqlConnection(Class1.connection); command = new SqlCommand(commandString, conn); // Add the parameters for the SelectCommand. SqlParameter table = new SqlParameter(); command.Parameters.Add("@pthis", SqlDbType.NVarChar, 100); command.Parameters.Add("@tables", SqlDbType.NVarChar, 100); //Add values to these parameters command.Parameters["@tables"].Value = "INFORMATION_SCHEMA.TABLES"; command.Parameters["@pthis"].Value = "TABLE_SCHEMA"; conn.Open(); reader = command.ExecuteReader(); GridView1.DataSource = reader; GridView1.DataBind(); // DropDownList1.DataSource = reader; // DropDownList1.DataTextField = "TABLE_NAME"; // DropDownList1.DataValueField = "TABLE_NAME"; // DropDownList1.DataBind(); reader.Close(); reader.Dispose(); conn.Close(); conn.Dispose(); }//try ends here. catch (SqlException ex) { try { reader.Close(); reader.Dispose(); conn.Close(); conn.Dispose(); } catch (Exception az) { Response.Write(az.Message); } Class1 object1 = new Class1(); object1.errorMessages = new System.Text.StringBuilder(); for (int i = 0; i < ex.Errors.Count; i++) { object1.errorMessages.Append("\n135 \n" + "Index #" + i + "\n" + "Message: " + ex.Errors[i].Message + "\n" + "LineNumber: " + ex.Errors[i].LineNumber + "\n" + "Source: " + ex.Errors[i].Source + "\n" + "Procedure: " + ex.Errors[i].Procedure + "\n"); } Response.Write(object1.errorMessages.ToString()); }//sql catch ends here catch (Exception all) { Label1.Text = "153 all\n" + all.ToString(); try { reader.Close(); reader.Dispose(); conn.Close(); conn.Dispose(); } catch (Exception zx) { Label5.Text = "connection 192 " + zx.Message; } Response.Write(all.Message.ToString()); }//catch all ends }//select_table_names
+7

而你的問題是? – 2010-06-22 10:13:54

+0

你究竟想達到什麼目的? – Oded 2010-06-22 10:17:22

+0

好吧,從「我的願望」中選擇「我的願望」..我的願望是輸入 – user287745 2010-06-22 10:21:42

回答

1

SQL Server肯定不允許使用變量作爲表名。至於你的列名變量,它將返回表中結果的數量,但它會輸出TABLE_SCHEMA

+0

以及我需要一個WA來解決它,也使用參數方法aviod注射,所以請指導。爲什麼它會顯示結果數!我要求它選擇table_schema! – user287745 2010-06-22 10:28:33

+0

當您使用變量時,您正在創建一個新列,這就是爲什麼它會返回表中的所有結果。嘗試一下: DECLARE @table AS VARCHAR(20); SET @table ='TABLE_SCHEMA'; SELECT * FROM INFORMATION_SCHEMA.TABLES; – 2010-06-22 10:34:50

+0

o,所以上述是不可能的,你回答者是什麼......建議我應該這樣做,我不想要字符串和變量cancatenate,因爲這將不允許我檢查/強制數據類型。我必須爲用戶提供要選擇哪個表的選項,並且還要保存編碼 – user287745 2010-06-22 10:39:04

2

我不熟悉MS-SQL,但是我不認爲參數標記這樣

SELECT @pthis FROM @tables 

工作。

參數通常用來代替文字值,而不是標識符。

+0

這是正確的。 – Locksfree 2010-06-22 10:21:31

+0

「文字值,而不是標識符。」好的,請你描述一下你的意思是字面意思和標識符,它是一個querry,這個術語是從哪裏來的。不明白 – user287745 2010-06-22 10:23:02

+0

@ user287745他意味着你不能在變量中放置一個表名並且運行查詢。 SQL不會讓你。 – Meff 2010-06-22 10:29:38