dbcommand

Easier way to send parameterised query to database?

核能气质少年 提交于 2020-01-06 19:39:10
问题 Is there a way to write the following code in less lines? It seems like a lot of code to execute such a simple query. No LINQ as I am using VS2005. Answers in either VB or C# are acceptable. Using cmd As DbCommand = oDB.CreateCommand() cmd.CommandText = "SELECT * FROM [Table1] WHERE [Date] BETWEEN @Date1 AND @Date2" cmd.CommandTimeout = 30 cmd.CommandType = CommandType.Text cmd.Connection = oDB Dim param As DbParameter param = cmd.CreateParameter() param.Direction = ParameterDirection.Input

How to Convert a Nullable DateTime variable's null value to DbNull.Value

允我心安 提交于 2019-12-23 17:12:26
问题 I have a nullable DateTime Variable. And I want to write it to SQL DB. When i try to insert: If the variable has value there is no problem. But if it hasn't a value, insertion interrupting with an error. I want to ask: How can we insert nullable DateTime to Sql via DbCommand Parameter? (P.S. : Sql column is nullable too.) DateTime? myDate = null; DbCommand dbCommand = new DbCommand(); dbCommand.Parameters.Add("NullableSqlDateField", DbType.DateTime, myDate); 回答1: Try the null coalescing

Inserting NULL to SQL DB from C# DbCommand

别来无恙 提交于 2019-12-14 00:28:54
问题 DbParameter param = comm.CreateParameter(); param = comm.CreateParameter(); param.ParameterName = "@StaffId"; if (!string.IsNullOrEmpty(activity.StaffId)) param.Value = activity.StaffId; param.DbType = DbType.String; comm.Parameters.Add(param); The above does not work (obviously), object not instantiated. I am attempting to insert a NULL into the database when StaffId is NOT populated. How can I achieve this? 回答1: You can use DBNull.Value when you need to pass NULL as a parameter to the

SQL0666 - SQL query exceeds specified time limit or storage limit

本秂侑毒 提交于 2019-12-10 12:50:55
问题 Periodically, I get this error message while making a call to a DB2 database using the Odbc connection string. I have tried setting the CommandTimeout of the DbCommand object to multiple values, but I still get the following error. SQL0666 - SQL query exceeds specified time limit or storage limit. Is there a trick to getting this to stop erroring out. It is very odd because the same query sometimes will work and sometimes will timeout. Any help would be appreciated. Thanks! 回答1: I have tried

Inserting NULL to SQL DB from C# DbCommand

[亡魂溺海] 提交于 2019-12-04 22:49:13
DbParameter param = comm.CreateParameter(); param = comm.CreateParameter(); param.ParameterName = "@StaffId"; if (!string.IsNullOrEmpty(activity.StaffId)) param.Value = activity.StaffId; param.DbType = DbType.String; comm.Parameters.Add(param); The above does not work (obviously), object not instantiated. I am attempting to insert a NULL into the database when StaffId is NOT populated. How can I achieve this? You can use DBNull.Value when you need to pass NULL as a parameter to the stored procedure. param.Value = DBNull.Value; Or you can use that instead of your if operator: param.Value =

How to generate List<String> from SQL query?

試著忘記壹切 提交于 2019-12-03 05:53:52
If I have a DbCommand defined to execute something like: SELECT Column1 FROM Table1 What is the best way to generate a List<String> of the returned records? No Linq etc. as I am using VS2005. Chuck Callebs I think this is what you're looking for. List<String> columnData = new List<String>(); using(SqlConnection connection = new SqlConnection("conn_string")) { connection.Open(); string query = "SELECT Column1 FROM Table1"; using(SqlCommand command = new SqlCommand(query, connection)) { using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { columnData.Add(reader

How to generate List<String> from SQL query?

余生长醉 提交于 2019-11-30 13:47:17
问题 If I have a DbCommand defined to execute something like: SELECT Column1 FROM Table1 What is the best way to generate a List<String> of the returned records? No Linq etc. as I am using VS2005. 回答1: I think this is what you're looking for. List<String> columnData = new List<String>(); using(SqlConnection connection = new SqlConnection("conn_string")) { connection.Open(); string query = "SELECT Column1 FROM Table1"; using(SqlCommand command = new SqlCommand(query, connection)) { using

Is order of parameters for database Command object really important?

空扰寡人 提交于 2019-11-27 16:14:52
I was debugging a database operation code and I found that proper UPDATE was never happening though the code never failed as such. This is the code: condb.Open(); OleDbCommand dbcom = new OleDbCommand("UPDATE Word SET word=?,sentence=?,mp3=? WHERE id=? AND exercise_id=?", condb); dbcom.Parameters.AddWithValue("id", wd.ID); dbcom.Parameters.AddWithValue("exercise_id", wd.ExID); dbcom.Parameters.AddWithValue("word", wd.Name); dbcom.Parameters.AddWithValue("sentence", wd.Sentence); dbcom.Parameters.AddWithValue("mp3", wd.Mp3); But after some tweaking this worked: condb.Open(); OleDbCommand dbcom

Is order of parameters for database Command object really important?

微笑、不失礼 提交于 2019-11-26 22:26:46
问题 I was debugging a database operation code and I found that proper UPDATE was never happening though the code never failed as such. This is the code: condb.Open(); OleDbCommand dbcom = new OleDbCommand("UPDATE Word SET word=?,sentence=?,mp3=? WHERE id=? AND exercise_id=?", condb); dbcom.Parameters.AddWithValue("id", wd.ID); dbcom.Parameters.AddWithValue("exercise_id", wd.ExID); dbcom.Parameters.AddWithValue("word", wd.Name); dbcom.Parameters.AddWithValue("sentence", wd.Sentence); dbcom