sqlcommand

Using SqlCommand , how to add multiple parameters to its object , insertion via winform in sql table

瘦欲@ 提交于 2019-11-29 04:16:22
问题 I have ten textboxes in my winform, and i need to save the text typed in these textboxes into 10 columns of a sql database table. so, for this shall i write : INSERT INTO item (c1,c2,c3...,c10) values (@a,@b....@j) cmd.Parameters.Add("@a",SqlDbType.Varchar) cmd.Parameteres["@a"].Value=textbox1.Text; cmd.Parameters.Add("@b",SqlDbType.Varchar) cmd.Parameteres["@b"].Value=textbox2.Text;. . . . . cmd.Parameters.Add("@j",SqlDbType.Varchar) cmd.Parameteres["@j"].Value=textbox10.Text; OR ten

Dynamic where clause in parameter

℡╲_俬逩灬. 提交于 2019-11-28 14:20:21
问题 I am currently trying to build up the where clause of an SqlCommand . something similar to this myCommand.CommandText = "SELECT * " + "FROM TABLE1 " + "@whereClause"; //I build up the where clause with a StringBuilder myCommand.Parameters.AddWithValue("@whereClause" theClause.ToString()); But it doesn't seem like this is possible. I got the exception : SqlException Incorrect syntax near '@whereClause' The reason I want to do something like this is because I want to avoid X call to the

How to pass variable into SqlCommand statement and insert into database table

£可爱£侵袭症+ 提交于 2019-11-28 14:13:56
I'm writing a small program in C# that uses SQL to store values into a database at runtime based on input by the user. The only problem is I can't figure out the correct Sql syntax to pass variables into my database. private void button1_Click(object sender, EventArgs e) { int num = 2; using (SqlCeConnection c = new SqlCeConnection( Properties.Settings.Default.rentalDataConnectionString)) { c.Open(); string insertString = @"insert into Buildings(name, street, city, state, zip, numUnits) values('name', 'street', 'city', 'state', @num, 332323)"; SqlCeCommand cmd = new SqlCeCommand(insertString,

From .NET can I get the full SQL string generated by a SqlCommand object (with SQL Parameters)?

心不动则不痛 提交于 2019-11-28 13:40:26
From the .NET environment can I get access to the full SQL string that is generated by a SqlCommand object? Note: The full SQL string shows up in Intellisense hover, in VisualStudio, while in debug mode. I'm willing to use reflection techniques if I must. I'm sure somebody here knows a way to get at it. Update 1 : I'm calling a stored procedure having parameters with cmd.CommandType = CommandType.StoredProcedure and am trying to acquire the full SQL generated and run. I wonder if the cmd. Prepare() method might not prove useful in this circumstance, if it might store the full string in a state

When would SqlCommand.ExecuteReader() return null?

感情迁移 提交于 2019-11-28 13:21:00
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 reader object ever be null? I've never come across it and the documentation doesn't mention that it

SqlCommand() ExecuteNonQuery() truncates command text

寵の児 提交于 2019-11-28 13:18:18
I'm building a custom db deployment utility, I need to read text files containing sql scripts and execute them against the database. Pretty easy stuff, so far so good. However I've encountered a snag, the contents of the file are read successfully and entirely, but once passed into the SqlCommand and then executed with SqlCommand.ExecuteNonQuery only part of the script is executed. I fired up Profiler and confirmed that my code is not passing all of the script. private void ExecuteScript(string cmd, SqlConnection sqlConn, SqlTransaction trans) { SqlCommand sqlCmd = new SqlCommand(cmd, sqlConn,

Is it possible to get the parsed text of a SqlCommand with SqlParameters?

∥☆過路亽.° 提交于 2019-11-28 11:58:27
What I am trying to do is create some arbitrary sql command with parameters, set the values and types of the parameters, and then return the parsed sql command - with parameters included. I will not be directly running this command against a sql database, so no connection should be necessary. So if I ran the example program below, I would hope to see the following text (or something similar): WITH SomeTable (SomeColumn) AS ( SELECT N':)' UNION ALL SELECT N'>:o' UNION ALL SELECT N'^_^' ) SELECT SomeColumn FROM SomeTable And the sample program is: using System; using System.Data; using System

Data Adapter Vs Sql Command

随声附和 提交于 2019-11-28 10:23:10
Which one would be better in executing an insert statement for ms-sql database: Sql DataAdapter or SQL Command Object? Which of them would be better, while inserting only one row and while inserting multiple rows ? A simple example of code usage: SQL Command string query = "insert into Table1(col1,col2,col3) values (@value1,@value2,@value3)"; int i; SqlCommand cmd = new SqlCommand(query, connection); // add parameters... cmd.Parameters.Add("@value1",SqlDbType.VarChar).Value=txtBox1.Text; cmd.Parameters.Add("@value2",SqlDbType.VarChar).Value=txtBox2.Text; cmd.Parameters.Add("@value3",SqlDbType

SqlCommand read one value

醉酒当歌 提交于 2019-11-28 10:05:14
问题 I have a problem with the value returned from SqlCommand , I have this code: string sqlSelect = "Select TOP 1 Quotation.SentToSupp as SentToSupp FROM Quotation JOIN Notifications ON Quotation.QuotationId = QuotationID "; SqlCommand Comm = new SqlCommand(sqlSelect, this.Connection); sqlSelect query selects the first DateTime value. When I call to SqlCommand I want to get this value (just one value). I add the query and my connection fine. But I don't know how to get my DateTime value... Must

How do I run large SQL scripts that contain many keywords, including “GO” using C#?

蓝咒 提交于 2019-11-28 09:58:36
问题 I'm creating a web application that serves as a front end to do SQL Replication. I have many scripts stored in the properties of the program. Let's use the first one as an example. The first script is the script that you get from creating a publication on the publisher server. USE [<<SOURCE_DATABASE_NAME>>] EXEC sp_replicationdboption @dbname = N'<<SOURCE_DATABASE_NAME>>', @optname = N'publish', @value = N'true' GO USE [<<SOURCE_DATABASE_NAME>>] EXEC [<<SOURCE_DATABASE_NAME>>].sys.sp