ado.net

Configure a custom data type mapping for use with a System.Data.SqlClient.SqlParameter

百般思念 提交于 2019-12-23 14:03:19
问题 I have a struct called CaseInsensitiveString which is just a wrapper that allows my program to work with strings without caring about their case. When it is persisted to a DB, it is no different from a regular string, though, so I want to be able to use it with a System.Data.SqlClient.SqlParameter like this: var myString = new CaseInsensitiveString("foo"); var param = new SqlParameter("@MyValue", myString); Of course I can't do that because SqlParameter doesn't know about my

How to Catch an exception in a using block with .NET 2.0?

回眸只為那壹抹淺笑 提交于 2019-12-23 13:03:12
问题 I'm trying to leverage the using block more and more these days when I have an object that implements IDisposable but one thing I have not figured out is how to catch an exception as I would in a normal try/catch/finally ... any code samples to point me in the right direction? Edit: The question was modified after reading through the replies. It was "How to Throw an exception in a using block with .NET 2.0?" but I was actually looking for a way to catch these exceptions inside a using block.

How to check state before disposing SqlTransaction

我的未来我决定 提交于 2019-12-23 12:20:16
问题 I have following code that uses SqlTransaction. I have called dispose in catch and finally blocks.. But I have not checked whether it is already disposed before calling Dispose(). How can we check whether the SqlTransaction is already disposed before calling Dispose()? I have referred MSDN:SqlTransaction.Dispose Method. But that does not cover my question. Also referred http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqltransaction.dispose(v=vs.100).aspx Note: I already know

C# How can I get each column type and length and then use the lenght to padright to get the spaces at the end of each field

非 Y 不嫁゛ 提交于 2019-12-23 12:19:18
问题 I have a console application that extracts data from a SQL table to a flat file. How can I get each column type and length and then use the lenght of each column to padright(length) to get the spaces at the end of each field. Here is what I have right now that does not include this functionality. Thanks { var destination = args[0]; var command = string.Format("Select * from {0}", Validator.Check(args[1])); var connectionstring = string.Format("Data Source={0}; Initial Catalog=dbname

How to detect EOF on DataReader in C# without executing Read()

若如初见. 提交于 2019-12-23 12:16:10
问题 I'm familiar with using .Read() to detect the EOF using (IDataReader reader = SqlHelper.ExecuteReader(_connectionString, "dbo.GetOrders")) { AssertOrder(reader); while (reader.Read()) { yield return FillRecord<Order>(reader, StringComparer.OrdinalIgnoreCase); } reader.Close(); } Due to some weird situation I got myself into, the FillRecord actually advances the reader. So now the .Read() in the while loop actually causes this function to skip some rows -- because we are advancing twice. I

Problem reading out parameter from stored procedure using c#

☆樱花仙子☆ 提交于 2019-12-23 12:04:20
问题 I just come across a strange problem where i cannot retrieve the sql stored procedure out parameter value. I struck with this problem for nearly 2 hours. Code is very simple using (var con = new SqlConnection(connectionString)) { con.Open(); SqlCommand cmd = new SqlCommand("sp_mgsearach", con); cmd.CommandType = CommandType.StoredProcedure; SqlParameter param1 = new SqlParameter("@SearchTerm", SqlDbType.VarChar); param1.Value = searchTerm; param1.Direction = ParameterDirection.Input; cmd

Delete with params in SqlCommand

做~自己de王妃 提交于 2019-12-23 12:02:04
问题 I use ADO.NET to delete some data from DB like this: using (SqlConnection conn = new SqlConnection(_connectionString)) { try { conn.Open(); using (SqlCommand cmd = new SqlCommand("Delete from Table where ID in (@idList);", conn)) { cmd.Parameters.Add("@idList", System.Data.SqlDbType.VarChar, 100); cmd.Parameters["@idList"].Value = stratIds; cmd.CommandTimeout = 0; cmd.ExecuteNonQuery(); } } catch (Exception e) { //_logger.LogMessage(eLogLevel.ERROR, DateTime.Now, e.ToString()); } finally {

Error: The conversion of a nvarchar data type to a smalldatetime data type resulted in an out-of-range value

时光毁灭记忆、已成空白 提交于 2019-12-23 11:20:30
问题 Hey all I'm trying to do the following insert query SqlDataSource userQuizDataSource = new SqlDataSource(); userQuizDataSource.ConnectionString = "Data Source=localhost\\SQLEXPRESS;Initial Catalog=quizApp;Integrated Security=True"; userQuizDataSource.InsertCommand = "INSERT INTO [UserQuiz] ([DateTimeComplete], [Score], [UserName]) VALUES (@DateTimeComplete, @Score, @UserName)"; userQuizDataSource.InsertParameters.Add("DateTimeComplete", DateTime.Now.ToString()); userQuizDataSource

ExecuteReader returns no results, when inspected query does

久未见 提交于 2019-12-23 10:47:49
问题 Consider the following code: StringBuilder textResults = new StringBuilder(); using(SqlConnection connection = new SqlConnection(GetEntityConnectionString())) { connection.Open(); m.Connection = connection; SqlDataReader results = m.ExecuteReader(); while (results.Read()) { textResults.Append(String.Format("{0}", results[0])); } } I used Activity Monitor within Sql Server Mgmt Studio on the database to inspect the exact query that was being sent. I then copied that query text to a query

OLE CALL to Excel with WHERE clause

删除回忆录丶 提交于 2019-12-23 10:42:30
问题 I am working on fixing a C# project which uses an Excel sheet with data definition to create a .fmt file used by a bulk insert in SQL Server. My issue is that the Excel files sometimes have blank rows at the bottom and the C# parser I am working on will detect a number of rows superior to the actual number of rows containint data definition. The fmt file therefore has a larger number of rows on its second line and bulk insert throws an exception when it reaches the bottom and tries to read on