sqlcommand

C# Update Table using SqlCommand.Parameters ASP.NET [duplicate]

删除回忆录丶 提交于 2019-11-28 06:44:28
问题 Possible Duplicate: C# Update Table using SqlCommand.Parameters I'm trying to update an SQL Server table using SqlCommand, I think it's a syntax error with my T-SQL, but here is what I have so far: SqlCommand sqlCmd = new SqlCommand( "UPDATE yak_tickets SET email = @emailParam, subject = @subjectParam, text = @textParam, statusid = @statusIDParam, ticketClass = @ticketClassParam WHERE id = @ticketIDParam", sqlConn); The parameters are working as they should, however, the table never gets

Turning a SqlCommand with parameters into a DataTable

余生颓废 提交于 2019-11-28 06:43:38
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? Use the DataTable.Load method to fill your table with values from the SqlDataReader: using (SqlDataReader dr = command.ExecuteReader()) { var tb = new DataTable(); tb.Load(dr); return tb; } By using a DBDataAdapter excerpt from ms documentation // Create the DbDataAdapter.

Save byte array in sql server

安稳与你 提交于 2019-11-28 02:32:39
问题 Am looking to use an approach in saving passwords that requires using byte array as in this post So which data type should i use in sql server to save byte array? and how can i pass and retrieve the byte array using SqlCommand? 回答1: If it's always going to be the same length, then binary(length) would be suitable. If it's going to vary in length, use varbinary(maxlength) . binary and varbinary. And, as @p.s.w.g says, you pass it from code by placing it into a suitable parameter. 回答2: Just use

SqlCommand INSERT INTO query does not execute

人走茶凉 提交于 2019-11-28 02:26:01
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 SQL table klient Missing the ExecuteNonQuery call SqlCommand prikaz = new SqlCommand("INSERT INTO

What does SqlCommand.Prepare() do and when should it be used? [duplicate]

半腔热情 提交于 2019-11-28 01:48:51
Possible Duplicate: Pros and Cons of using SqlCommand Prepare in C#? This is what MSDN says about SqlCommand.Prepare() : Creates a prepared version of the command on an instance of SQL Server. Can anybody provide more insight as to what that means and when it should be used? The Prepare method is actually on DbCommand , which all classes which derive from it will pick up. What it does is specific to the database provider that the DbCommand is for. However, it's safe to say (although not an absolute rule) that in most places, if the command is a stored procedure, it will produce a no op (it is

Is SqlConnection / SqlCommand thread safe?

我怕爱的太早我们不能终老 提交于 2019-11-28 01:12:48
问题 I am currently creating a WCF web service. As part of its job, it will unfortunately need to do some fairly intensive computations, however these computations can fortunately be shared between calls to the webservice. In effect - we only need to do the computations once, and all later calls can get the benefit. However, since WCF has no shared application state it seems logical to set WCF in single-instance mode. (Each client would require some of the computations in all likelyhood, forcing

In Java, send commands to another command-line program

天大地大妈咪最大 提交于 2019-11-28 00:06:43
问题 I am using Java on Windows XP and want to be able to send commands to another program such as telnet. I do not want to simply execute another program. I want to execute it, and then send it a sequence of commands once it's running. Here's my code of what I want to do, but it does not work: (If you uncomment and change the command to "cmd" it works as expected. Please help.) This is a simplified example. In production there will be many more commands sent, so please don't suggest calling

How to set isolation level on SqlCommand/SqlConnection initialized with no transaction

北战南征 提交于 2019-11-27 21:57:36
The following method is supposed to peroform a dirty read on an open connection. There are no transactions. Where do I set IsolationLevel? public string DoDirtyRead(string storedProcName, SqlConnection connection) { using (SqlCommand command = new SqlCommand(storedProcName, connection)) { command.CommandType = CommandType.StoredProcedure; // HOW TO SET IsolationLevel to READ_UNCOMMITTED here? command.ExecuteNonQuery(); } } On the BeginTransaction method: ( MSDN link ) And if you just want to use hints in your SP at the table level, use WITH(NOLOCK) - but use at your own risk. If you don't want

poor performance with sqlparameter

强颜欢笑 提交于 2019-11-27 16:27:58
问题 I have a web service, so the handler is called multiple times concurrently all the time. Inside I create SqlConnection and SqlCommand. I have to execute about 7 different commands. Different commands require various parameters, so I just add them once: command.Parameters.Add(new SqlParameter("@UserID", userID)); command.Parameters.Add(new SqlParameter("@AppID", appID)); command.Parameters.Add(new SqlParameter("@SID", SIDInt)); command.Parameters.Add(new SqlParameter("@Day", timestamp.Date));

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

ε祈祈猫儿з 提交于 2019-11-27 14:42:31
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("NonConnectionString"))) { using (SqlCommand sqlCommand = new SqlCommand("SELECT * from users where user_name like