sqlcommand

F# best way to set up a SQLCommand with parameters

扶醉桌前 提交于 2019-12-12 19:13:11
问题 My F# program needs to talk to SQL Server. In one part I have something like this: let workFlowDetailRuncommand = new SqlCommand(query, econnection) workFlowDetailRuncommand.CommandTimeout <- 100000 workFlowDetailRuncommand.Parameters.Add("@1", SqlDbType.Int).Value <- 42 workFlowDetailRuncommand.Parameters.Add("@2", SqlDbType.VarChar).Value <- "answer" workFlowDetailRuncommand.Parameters.Add("@3", SqlDbType.VarChar).Value <- mydate.ToString("yyyy.MM.dd") workFlowDetailRuncommand.Parameters

What would cause/how can I prevent a ContextSwitchDeadlock?

北城以北 提交于 2019-12-12 18:39:26
问题 I have a rather long-running process in a Windows Service that periodically throws a "ContextSwitchDeadlock" exception: I have also rigged my service to send myself emails with details about encountered exceptions. I get: Date: 05/25/2016 09:16:32: Exception message: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. Exception Source: .Net SqlClient Data Provider ...and then three seconds later this: Date: 05/25/2016 09:16:35:

How to insert a c# datetime var into SQL Server

余生颓废 提交于 2019-12-12 17:00:58
问题 Here is the code: string ConnectionString= @"Data Source=localhost\SQLEXPRESS; Initial Catalog=notepad; Integrated Security=SSPI "; SqlConnection con = new SqlConnection(ConnectionString); con.Open(); string strEvent = TextBoxEvent.Text; string strDate = Calendar1.TodaysDate.ToShortDateString(); string strInsert = "insert into notepad (time, event) values (strDate, strEvent )"; SqlCommand cmd=new SqlCommand(strInsert, con); cmd.ExecuteNonQuery(); the time is smalldatetime in SQL Server 2005

What SqlCommand.Parameters.AddWithValue really does?

旧巷老猫 提交于 2019-12-12 16:18:52
问题 What changes SqlCommand.Parameters.AddWithValue() does with the query? I expect that: It replaces every ' character by '' , If a parameter value is a string or something which must be converted to a string, it surrounds the value by ' , so for example select * from A where B = @hello will give select * from A where B = 'hello world' . If a parameter value is something "safe" like an integer, it is inserted in a query as is, without quotes, so select * from A where B = @one would give select *

Why am I getting “timeout” and “Cannot find table 0” and what preventive measures are at my disposal?

六眼飞鱼酱① 提交于 2019-12-12 05:07:38
问题 In the answer to a previous question here, I was advised to also ask about this related issue. Once in awhile, the report generation code I have throws two exceptions (they don't display to the user, and they think everything is hunky dory), but I get them emailed to me. The first is, " Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. " ...and the one that always follows quickly thereafter is, " Cannot find table 0 " The first

Insert user defined variables in to the sql statements [duplicate]

流过昼夜 提交于 2019-12-11 20:12:54
问题 This question already has answers here : SqlCommand with Parameters (3 answers) Closed 5 years ago . Hi this is my query SELECT StraightDist FROM StraightLineDistances WHERE (FirstCity='007' AND SecondCity='017'); How can I pass this in to sql statement? I want to replace the city numbers '007' and '017' with variables string destcity; string tempcityholder1; What I tried is this SqlCommand mybtncmd2 = new SqlCommand("SELECT StraightDist FROM StraightLineDistances WHERE (FirstCity='" +

Insert date in dd-MM-yyyy format

最后都变了- 提交于 2019-12-11 19:59:28
问题 I'm trying to insert date in dd-MM-yyyy format in c#. Query for inserting is SqlCommand cmd_cust = new SqlCommand(@"insert into custdetail values ('" + txtInvoiceNo.Text + "','" + txtCustomerName.Text + "','" + txt_contact.Text + "', '" + txtAddress.Text + "', '" + txt_total_amt.Text + "', '" + dt_date.Value.ToString("dd-MM-yyyy") + "')", con_create); con_create.Open(); cmd_cust.ExecuteNonQuery(); con_create.Close(); I have created table with column name date has datatype date . After

SqlCommand.ExecuteScalar returns null but raw SQL does not

邮差的信 提交于 2019-12-11 18:34:54
问题 I have the following code that uses the SqlClient.ExecuteScalar method to return an ID from a table. using (var conn = new SqlConnection(connectionString)) using (var cmdContrib = new SqlCommand("SELECT ContributorId FROM Contributor WHERE Code='" + folderSystem.ContributorCode + "'", conn)) { conn.Open(); var contribId = cmdContrib.ExecuteScalar(); } Originally it was working but now contribId is null. I tested the SQL in management studio after extracting from Profiler and it returned the

Getting timeout errors with SqlTransaction on same table

删除回忆录丶 提交于 2019-12-11 18:03:10
问题 public TransImport() { ConnString = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString; SqlConnection conn_new; SqlCommand command_serial_new; SqlConnection conn; SqlCommand command_serial; SqlTransaction InsertUpdateSerialNumbers; conn = new SqlConnection(ConnString); command_serial = conn.CreateCommand(); conn_new = new SqlConnection(ConnString); command_serial_new = conn_new.CreateCommand(); command_serial_new.CommandText = "SELECT 1 FROM YSL00 WHERE SERLNMBR = @slnr";

Would executing the SqlCommand be completely moot/redundant, and slow things down?

被刻印的时光 ゝ 提交于 2019-12-11 13:18:21
问题 Thanks to some tips and reminders here, I changed my code from this kludgy mess: try { DataSet dsUsage = new DataSet(); SqlConnection conn = new SqlConnection("SERVER=PROSQL05;DATABASE=platypusdata;UID=duckbill;PWD=poisonToe42;Connection Timeout=0"); SqlDataAdapter da = new SqlDataAdapter(); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = String.Format("Exec sp_ViewProductUsage_MappingRS '{0}', '{1}', '{2}'", mammal, dateBegin, dateEnd); da.SelectCommand = cmd; conn.Open(); da.Fill