Questions tagged «ado.net»

程序员通常使用ADO.Net来访问和修改存储在关系数据库系统中的数据,尽管它也可以访问非关系源中的数据。它是Microsoft .NET Framework附带的基类库的一部分。


4
AddWithValue参数为NULL时发生异常
我有以下代码用于指定SQL查询的参数。我在使用时遇到了异常Code 1;但是我使用时工作正常Code 2。在这里,Code 2我们检查是否为null,因此是否为if..else块。 例外: 参数化查询'{@application_ex_id nvarchar(4000))SELECT E.application_ex_id A'期望未提供参数'@application_ex_id'。 代码1: command.Parameters.AddWithValue("@application_ex_id", logSearch.LogID); 代码2: if (logSearch.LogID != null) { command.Parameters.AddWithValue("@application_ex_id", logSearch.LogID); } else { command.Parameters.AddWithValue("@application_ex_id", DBNull.Value ); } 题 您能否解释一下为什么它无法从代码1中的logSearch.LogID值中获取NULL(但能够接受DBNull)? 有更好的代码来处理吗? 参考: 将空值分配给SqlParameter 返回的数据类型因表中的数据而异 从数据库smallint到C#可为空的int的转换错误 DBNull的意义是什么? 码 public Collection<Log> GetLogs(LogSearch logSearch) { Collection<Log> logs = new Collection<Log>(); using (SqlConnection connection = …
89 c#  .net  ado.net 

7
如何使用SqlConnectionStringBuilder从连接字符串获取数据库名称
我从不想使用字符串操作来拆分连接字符串并获取服务器,数据库,uid和密码。 我阅读了以下链接并阅读了接受的答案,我发现这是从连接字符串中获取用户名和密码的最佳方法,但是数据库名称呢? 从连接字符串获取用户名和密码的正确方法? 如何使用SqlConnectionStringBuilder从连接字符串获取数据库名称。(数据源是服务器名称吗?)
87 c#  asp.net  sql  ado.net 



10
SqlParameter已被另一个SqlParameterCollection包含-using(){}是否作弊?
using() {}如下所示使用(sic)块时,并假设该块cmd1没有超出第一个using() {}块的范围,为什么第二个块会在消息中引发异常 SqlParameter已被另一个SqlParameterCollection包含 这是否意味着在块末尾销毁的资源和/或句柄(包括参数(SqlParameterCollection))cmd1不会释放? using (var conn = new SqlConnection("Data Source=.;Initial Catalog=Test;Integrated Security=True")) { var parameters = new SqlParameter[] { new SqlParameter("@ProductId", SqlDbType.Int ) }; using(var cmd1 = new SqlCommand("SELECT ProductName FROM Products WHERE ProductId = @ProductId")) { foreach (var parameter in parameters) { cmd1.Parameters.Add(parameter); } // cmd1.Parameters.Clear(); // …
85 c#  .net  sql-server  ado.net 

4
调试时如何查看数据表
我刚刚开始使用ADO.NET,DataSet和DataTables。我遇到的一个问题是,在尝试调试时很难分辨数据表中的值。 快速查看数据表中已保存哪些值的最简单方法是什么?在调试时是否有办法在Visual Studio中查看内容,还是将数据写出到文件的唯一选择? 我创建了一个小的实用程序函数,该函数会将DataTable写出到CSV文件中。但是,创建的结果CSV文件已被切断。大约距离写出System.Guid的最后一行大约3行。文件只是停止。我无法确定这是我的CSV转换方法还是DataTable的原始填充问题。 更新资料 忘记我刚刚忘记刷新流编写器的最后一部分。

9
用返回值调用存储过程
Наэтотвопросестьответына堆栈溢出нарусском:Какпривызовехранимойпроцедурыполучитьзначениепереданное в RETURN 我正在尝试从C#Windows应用程序调用存储过程。该存储过程在SQL Server 2008的本地实例上运行。我可以调用该存储过程,但无法从该存储过程中检索回该值。该存储过程应该返回序列中的下一个数字。我已经在网上进行了研究,并且我所看到的所有站点都指出该解决方案有效。 存储过程代码: ALTER procedure [dbo].[usp_GetNewSeqVal] @SeqName nvarchar(255) as begin declare @NewSeqVal int set NOCOUNT ON update AllSequences set @NewSeqVal = CurrVal = CurrVal+Incr where SeqName = @SeqName if @@rowcount = 0 begin print 'Sequence does not exist' return end return @NewSeqVal end 调用存储过程的代码: SqlConnection conn …

6
没有数据时读取尝试无效
private void button1_Click(object sender, EventArgs e) { string name; name = textBox5.Text; SqlConnection con10 = new SqlConnection("con strn"); SqlCommand cmd10 = new SqlCommand("select * from sumant where username=@name"); cmd10.Parameters.AddWithValue("@name",name); cmd10.Connection = con10; cmd10.Connection.Open();//line 7 SqlDataReader dr = cmd10.ExecuteReader(); } if ( textBox2.Text == dr[2].ToString()) { //do something; } 当我调试到第7行时,还可以,但是在那之后dr抛出异常: Invalid …
80 c#  ado.net 

7
如何从数据表中提取数据?
我有一个DataTable从SQL查询到本地数据库的数据,但是我不知道如何从中提取数据。主要方法(在​​测试程序中): static void Main(string[] args) { const string connectionString = "server=localhost\\SQLExpress;database=master;integrated Security=SSPI;"; DataTable table = new DataTable("allPrograms"); using (var conn = new SqlConnection(connectionString)) { Console.WriteLine("connection created successfuly"); string command = "SELECT * FROM Programs"; using (var cmd = new SqlCommand(command, conn)) { Console.WriteLine("command created successfuly"); SqlDataAdapter adapt = new SqlDataAdapter(cmd); …
78 c#  sql  ado.net 


4
在SQL LIKE子句中使用SqlParameter不起作用
我有以下代码: const string Sql = @"select distinct [name] from tblCustomers left outer join tblCustomerInfo on tblCustomers.Id = tblCustomerInfo.CustomerId where (tblCustomer.Name LIKE '%@SEARCH%' OR tblCustomerInfo.Info LIKE '%@SEARCH%');"; using (var command = new SqlCommand(Sql, Connection)) { command.Parameters.AddWithValue("@SEARCH", searchString); ... } 这行不通,我也尝试这样做: const string Sql = @"select distinct [name] from tblCustomers left outer …


6
如何排序给定列和方向的数据表?
我需要在内存中使用基于GridView的列和方向的DataTable。该函数需要如下所示: public static DataTable resort(DataTable dt, string colName, string direction) { DataTable dtOut = null; .... } 我需要填写此功能的帮助。我认为我可以使用Select语句,但不确定如何使用。由于使用此浏览器,我无法单击“注释”,但是您可以向我展示就地或新的DataTable解决方案,无论是哪种解决方案。对于那些向我展示指针的人,我需要一个类似于原型的编码函数。 怎么样: // ds.Tables[0].DefaultView.Sort="au_fname DESC"; public static void Resort(ref DataTable dt, string colName, string direction) { string sortExpression = string.Format("{0} {1}", colName, direction); dt.DefaultView.Sort = sortExpression; }
71 c#  ado.net  datatable 

9
Microsoft.ACE.OLEDB.12.0提供程序未注册
我有一个带有两个项目的Visual Studio 2008解决方案(一个Word-Template项目和一个用于测试的VB.Net控制台应用程序)。这两个项目都引用一个数据库项目,该项目打开了与MS-Access 2007数据库文件的连接,并引用了System.Data.OleDb。在数据库项目中,我有一个检索数据表的功能,如下所示 private class AdminDatabase ' stores the connection string which is set in the New() method dim strAdminConnection as string public sub New() ... adminName = dlgopen.FileName conAdminDB = New OleDbConnection conAdminDB.ConnectionString = "Data Source='" + adminName + "';" + _ "Provider=Microsoft.ACE.OLEDB.12.0" ' store the connection string …

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.