SqlCommand.Prepare Method

SqlCommand.Prepare Method

Remarks

If CommandType is set to StoredProcedure, the call to Prepare should succeed, although it may cause a no-op.

Before you call Prepare, specify the data type of each parameter in the statement to be prepared. For each parameter that has a variable length data type, you must set the Size property to the maximum size needed. Prepare returns an error if these conditions are not met.

Note

If the database context is changed by executing the Transact-SQL USE <database> statement, or by calling the ChangeDatabase method, then Prepare must be called a second time.

If you call an Execute method after calling Prepare, any parameter value that is larger than the value specified by the Size property is automatically truncated to the original specified size of the parameter, and no truncation errors are returned.

Output parameters (whether prepared or not) must have a user-specified data type. If you specify a variable length data type, you must also specify the maximum Size.

Prior to Visual Studio 2010, Prepare threw an exception. Beginning in Visual Studio 2010, this method does not throw an exception.

 private static void SqlCommandPrepareEx(string connectionString)
        {
            try
            {
                DataTable dataTable = new DataTable();
                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    connection.Open();
                    SqlCommand command = new SqlCommand(null, connection);

                    // Create and prepare an SQL statement.
                    command.CommandText =
                        @"SELECT *
                FROM dbo.tbm_mem_Member_Employee
                WHERE PreferredEmail = @email";
                    SqlParameter idParam = new SqlParameter("@email", SqlDbType.VarChar,4000);

                    idParam.Value = "Test2259.TFL@rdc.com";
                    //command.Parameters.Add("@email", SqlDbType.VarChar).Value = "Test2259.TFL@rdc.com";
                    command.Parameters.Add(idParam);

                    // Call Prepare after setting the Commandtext and Parameters.
                    command.Prepare();
                    SqlDataAdapter da = new SqlDataAdapter(command);
                    // this will query your database and return the result to your datatable
                    da.Fill(dataTable);
                    connection.Close();
                    da.Dispose();
                }

                Console.WriteLine(dataTable.Rows.Count);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

        [Test]

        public void SQlPrepareTest()
        {
            string str = "database=UK_Connect_6;server=172.31.211.120\\sql2017;UID=clu;PWD=bqC@zwLuyul@1aaH;Current Language=English;Connection Timeout=120;";
            SqlCommandPrepareEx(str);
        }

 

SqlCommand.Prepare Method

上一篇:Photoshop插画作品详细制作教程及思路详解


下一篇:用Photoshop简单打造照片素雅风格