SQL сходит с ума!не принимает параметры? - PullRequest
0 голосов
/ 22 июня 2010

альтернативный текст http://img24.imageshack.us/img24/3365/sqlnuts.jpg

ну вот код


 public void select_table_names()
        {//start</p>

        /* 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

Ответы [ 2 ]

2 голосов
/ 22 июня 2010

Я не знаком с MS-SQL, но я не думаю, что такие маркеры параметров вот так

SELECT @pthis FROM @tables

работа.

Параметры обычно используются вместо литеральных значений, а не вместо идентификаторов.

1 голос
/ 22 июня 2010

SQL Server определенно не позволит использовать переменную в качестве имени таблицы.Что касается вашей переменной имени столбца, она вернет число результатов, которые имеет таблица, но она выдаст TABLE_SCHEMA.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...