sqlcommand

SqlCommand with using statement

ε祈祈猫儿з 提交于 2019-11-27 02:08:06
I saw that in most samples SqlCommand was used like this using (SqlConnection con = new SqlConnection(CNN_STRING)) { using (SqlCommand cmd = new SqlCommand("Select ID,Name From Person", con)) { SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); return ds; } } I know why we are using "using" statement. But SqlCommand doesn't inlcude Close() method, so should we really use it within using statement Because it also implements IDisposable . The purpose of Using statement is that when control will reach end of using it will dispose that object of using block and

How to run multiple SQL commands in a single SQL connection?

只愿长相守 提交于 2019-11-27 00:37:54
I am creating a project in which I need to run 2-3 sql commands in a single sql connection. Here is the code I have written: SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\project.mdf;Integrated Security=True"); con.Open(); SqlCommand cmd = new SqlCommand("select * from " + mytags.Text + " ", con); SqlDataReader rd = cmd.ExecuteReader(); if (rd.Read()) { con.Close(); con.Open(); SqlCommand cmd1 = new SqlCommand("insert into " + mytags.Text + " values ('fname.lname@gmail.com','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text

SqlCommand INSERT INTO query does not execute

╄→尐↘猪︶ㄣ 提交于 2019-11-26 23:43:38
问题 Hello guys I have got this code: SqlCommand scom = new SqlCommand( "INSERT INTO klient(name,surname) values(@kname,@ksurname)", conn); scom.Parameters.AddWithValue("@kname", kname.Text); scom.Parameters.AddWithValue("@ksurname", ksurname.Text); conn.Open(); DataTable dt = new DataTable(); SqlDataAdapter SDA = new SqlDataAdapter("SELECT * FROM klient", spojeni); SDA.Fill(dt); conn.Close(); It should insert data from textboxes: kname, ksurname, but it closes the form without showing them in MS

Insert into C# with SQLCommand

一世执手 提交于 2019-11-26 23:10:53
What's the best way to INSERT data into a database? This is what I have but it's wrong.. cmd.CommandText = "INSERT INTO klant(klant_id,naam,voornaam) VALUES(@param1,@param2,@param3)"; cmd.Parameters.Add(new SqlParameter("@param1", klantId)); cmd.Parameters.Add(new SqlParameter("@param2", klantNaam)); cmd.Parameters.Add(new SqlParameter("@param3", klantVoornaam)); The function add data into the listBox http://www.pictourl.com/viewer/37e4edcf (link is dead) but not into the database.. http://www.pictourl.com/viewer/4d5721fc (link is dead) The full function: private void Form1_Load(object sender,

What is the difference between SqlCommand.CommandTimeout and SqlConnection.ConnectionTimeout?

一世执手 提交于 2019-11-26 22:05:23
Is there any difference between SqlCommand.CommandTimeout and SqlConnection.ConnectionTimeout in .NET? Yes. CommandTimeout is how long a single command can take to complete. ConnectionTimeout is how long it can take to establish a connection to the server to start with. For instance, you may be executing relatively long-running queries - it's perfectly okay for them to take 10 minutes to complete, but if it took 10 minutes to make the connection to start with, you'd know that something was badly wrong. SqlCommand.CommandTimeout = timeout limit for your SQL query. Means, how much time a (eg:

How can I get an error message that happens when using ExecuteNonQuery()?

不羁的心 提交于 2019-11-26 21:13:56
问题 I am executing a command in this way : var Command = new SqlCommand(cmdText, Connection, tr); Command.ExecuteNonQuery(); In the command there is an error, however .NET does not throw any error message. How could I know that the command did not executed properly, and how to get the exception? 回答1: You'll only get an exception in C# if your error's severity is 16 or above. If you are using a PRINT, you won't get an exception in .NET. If you can edit the raise error code, this would cause a

Is SqlCommand.Dispose() required if associated SqlConnection will be disposed?

泪湿孤枕 提交于 2019-11-26 20:46:32
I usually use code like this: using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString)) { var command = connection.CreateCommand(); command.CommandText = "..."; connection.Open(); command.ExecuteNonQuery(); } Will my command automatically disposed? Or not and I have to wrap it into using block? Is it required to dispose SqlCommand ? Just do this: using(var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString)) using(var command = connection.CreateCommand()) { command.CommandText = "..."; connection

Check if record in a table exist in a database through ExecuteNonQuery

岁酱吖の 提交于 2019-11-26 18:26:58
问题 in my program i need to check if a record in the database already exists in the table using the if statement. using c# i am trying to do this through an sql connection. as i supposed that the ExecuteNonQuery(); command returns an integer value, if my supposing is true, i want to know what value is true to know that a certain record exists in the table or not. here's a sample of my code: using (SqlConnection sqlConnection = dbUtil.GetSqlConnection(dbUtil.GetConnectionStringByName(

MySqlCommand Command.Parameters.Add is obsolete

心已入冬 提交于 2019-11-26 17:58:47
I'm making an C# windows Form Application in visual studio 2010. That application is connecting to an mysql database, and I want to insert data in it. Now do I have this part of code: MySqlConnection connection; string cs = @"server=server ip;userid=username;password=userpass;database=databse"; connection = new MySqlConnection(cs); connection.Open(); MySqlCommand command = new MySqlCommand(); string SQL = "INSERT INTO `twMCUserDB` (`mc_userName`, `mc_userPass`, `tw_userName`, `tw_userPass`) VALUES ('@mcUserName', '@mcUserPass', '@twUserName', '@twUserPass')"; command.CommandText = SQL; command

How do I translate a List<string> into a SqlParameter for a Sql In statement? [duplicate]

无人久伴 提交于 2019-11-26 16:43:24
问题 This question already has answers here : Pass Array Parameter in SqlCommand (11 answers) Closed 3 years ago . I seem to be confused on how to perform an In statement with a SqlParameter . So far I have the following code: cmd.CommandText = "Select dscr from system_settings where setting in @settings"; cmd.Connection = conn; cmd.Parameters.Add(new SqlParameter("@settings", settingList)); reader = cmd.ExecuteReader(); settingsList is a List<string> . When cmd.ExecuteReader() is called, I get an