sqlcommand

When would SqlCommand.ExecuteReader() return null?

穿精又带淫゛_ 提交于 2019-12-17 20:44:21
问题 When using calling the SqlCommand.ExecuteReader() method, ReSharper tells me I have a possible NullReference exception when I use the SqlDataReader object afterwards. So with the following code: using (SqlConnection connection = GetConnection()) { using (SqlCommand cmd = connection.CreateCommand()) { cmd.CommandText = ; //snip using (SqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { //snip } } } } The while (reader.Read()) line is underlined. My question is when would the

Turning a SqlCommand with parameters into a DataTable

徘徊边缘 提交于 2019-12-17 18:11:49
问题 I'm adapting some code that someone else wrote and need to return a DataTable for time's sake. I have code like this: using (SqlCommand command = new SqlCommand(query, conn)) { //add parameters and their values using (SqlDataReader dr = command.ExecuteReader()) { return dr; } But what's the best way to return a datatable? 回答1: Use the DataTable.Load method to fill your table with values from the SqlDataReader: using (SqlDataReader dr = command.ExecuteReader()) { var tb = new DataTable(); tb

SqlCommand with using statement

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-17 06:54:02
问题 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 回答1: Because it also implements IDisposable. The purpose of

SqlCommand with using statement

拥有回忆 提交于 2019-12-17 06:52:19
问题 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 回答1: Because it also implements IDisposable. The purpose of

Insert into C# with SQLCommand

ⅰ亾dé卋堺 提交于 2019-12-17 04:34:08
问题 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:/

MySqlCommand Command.Parameters.Add is obsolete

北城余情 提交于 2019-12-17 04:07:32
问题 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`)

one sql command with two connection string

我与影子孤独终老i 提交于 2019-12-14 01:11:46
问题 I want to run this query in C# SELECT * FROM [FirstDataBase].[dbo].[table1] INNER JOIN [SecondDataBase].[dbo].[table2] and my code is : SqlConnection cn = new SqlConnection(myConnectionString); SqlCommand cmd = new SqlCommand(@"SELECT * FROM [FirstDataBase].[dbo].[table1] INNER JOIN [SecondDataBase].[dbo].[table2]"); cmd.Connection = cn; // here is my question !!! cn.Open(); int x = (int)cmd.ExecuteScalar(); but my query needs two connection string ... one for [FirstDataBase] and second for

What happens to Unicode in a System.Data.SQLCommand

别等时光非礼了梦想. 提交于 2019-12-14 00:55:32
问题 I have a SQLCommand : "Update Customers Set Name = @name where code = @code" and this code: cmd.Parameters[0].Value = "بهروز";//(some Unicode characters) cmd.Parameters[1].Value = 1; cmd.ExecuteNonQuery(); or this code: UpdateCommand.CommandText = "UPDATE [Customers] SET [Name] = @p1 WHERE (([Code] = @p2) AND ((@p3 = 1 AND [Name] IS NULL) OR ([Name] = @p4)))"; UpdateCommand.Parameters.Add("@p1", System.Data.SqlDbType.SmallInt, 0, "Name"); UpdateCommand.Parameters.Add("@p2", System.Data

How do I solve this compile error? [closed]

£可爱£侵袭症+ 提交于 2019-12-13 11:31:17
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 7 years ago . I'm getting this compile error message. How do I solve it? Error 28 'WindowsFormsApplication1.SqlCommand' does not contain a definition for 'Parameters'

How to show loading dialog while a long time query is executed?

自古美人都是妖i 提交于 2019-12-13 02:38:06
问题 I created a winforms app with .net 4.0. First, I created a LoadingForm with cancel button, that must stop execution query. I want to create an ExecuteHelper class and LoadingForm with cancellation query button. public partial class LoadingFrm : DevExpress.XtraEditors.XtraForm { private Action _cancel; public LoadingFrm(Action a) { _cancel = a; InitializeComponent(); this.FormBorderStyle = FormBorderStyle.None; } private void simpleButton1_Click(object sender, EventArgs e) { this.close();