Tag: ado.net

如何使LINQ to SQL使用在运行时修改的连接字符串?

我正在尝试在LINQ to SQL中使用连接字符串构建器(ADO.NET)时遇到一些困难。 让我告诉大家我正在做什么: app.config文件: 和forms的片段: ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings[“LoremIpsum”]; if (null != settings) { string connection = settings.ConnectionString; SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connection); // passwordTextBox being the control where joe the user actually // enters his credentials builder.Password = passwordTextBox.Text; } LINQTOSQLDataClassDataContext db = new LINQTOSQLDataClassDataContext(); // finally some rather anecdotic LINQ sentence here: […]

如何在ADO.Net实体模型中将具有外键的表更新到另一个表?

我有2个表,Table1有一个主键’CustomizationId’,而Table2有一个与之匹配的FK Customizationid。 表2没有主键。 我正在尝试从基于Web的表单添加新记录。 我试图将其保存到数据库,我收到一个错误: Customization customization = new Customization(); Code code = new Code(); customization.Name = CustomizationName.Text; customization.LastUpdated = DateTime.Now; code.Top = top_js.InnerText; code.Bottom = bottom_js.InnerText; //code.CustomizationId = customization.CustomizationId; customization.Code = code; entities.AddToCustomizations(customization); entities.SaveChanges(); 当我调用SaveChanges时,无论是否添加注释行,我都会收到错误。 Unable to update the EntitySet ‘Code’ because it has a DefiningQuery and no element exists in the element to […]

数据库插入错误:“字符串或二进制数据将被截断”

当我登录时,我将我的用户名存储在会话中。 我的要求是我想将我的用户名存储在我的数据库中。 这里我将它存储在username1 。 输入用户名后,我可以使用response.write()打印它,并且打印效果很好。 但是,当我将其存储在数据库中时,它会产生以下错误: ** sqlException未被用户代码处理 和cmd.ExecuteScalar()的exception; 字符串或二进制数据将被截断。 该语句已终止。** 以下是我的ado.net代码: using (SqlConnection con = new SqlConnection(“Data Source=.;database=testdb1;Integrated Security=SSPI”)) { con.Open(); // SqlCommand cmd = new SqlCommand(“delete from fileinfo where ID=” + Convert.ToInt32(Request.Params[“one”]), con); string uname = (string) Session[“fname”].ToString() + ” ” + Session[“lname”].ToString(); //Session[“fname”].ToString()+” “+Session[“lname”].ToString(); // Response.Write(uname); // uname = “sri hari”; uname […]

内部.Net Framework数据提供程序错误1

我正在使用Visual Studio 2012 Ultimate版本开发一个带有所有Service Pack,C#和.NET Framework 4.5的WinForm应用程序。 我得到这个例外: Internal .Net Framework Data Provider error 1 有了这个堆栈: en System.Data.ProviderBase.DbConnectionInternal.PrePush(Object expectedOwner) en System.Data.ProviderBase.DbConnectionPool.PutObject(DbConnectionInternal obj, Object owningObject) en System.Data.ProviderBase.DbConnectionInternal.CloseConnection(DbConnection owningObject, DbConnectionFactory connectionFactory) en System.Data.SqlClient.SqlConnection.CloseInnerConnection() en System.Data.SqlClient.SqlConnection.Close() en AdoData.TRZIC.DisposeCurrentConnection() en AdoData.TRZIC.Finalize() 在析构函数中: ~TRZIC() { DisposeCurrentConnection(); if (this.getCodeCmd != null) this.getCodeCmd.Dispose(); } private void DisposeCurrentConnection() { if (this.conn != null) […]

用entity framework执行自定义sql?

我需要执行一个自定义查询,它将被保存在数据库中的某个地方,我需要它返回数据表或数据集并将其绑定到gridview,它将自动生成列为true。 我的所有数据访问层都与entity framework完美配合,但对于某些特定场景,我需要这样做,我想知道我是否应该将ado.net与entity framework结合起来,或者如果EF能够以某种方式实现它

当分配给命令的连接处于挂起的本地trans时,ExecuteReader需要命令才能具有事务

我必须在单个事务中插入两个表,下面要执行的查询。 其次在SqlDataReader上获取exceptionread = comm.ExecuteReader(); public void SqlExecuteNonQuery(Customer obj) { //string query = “DECLARE @_customerID int “; string query1 = “INSERT INTO customer (customerName,customerSex,Email) VALUES (‘” + obj.name + “‘,'” + obj.sex + “‘,'” + obj.Email + “‘) “; //string query2 = “SET @_customerID =@@identity “; string query3 = “INSERT INTO customerDetails(customerID,customerAddress,customerPhone) VALUES (” + obj.id […]

如何在DataGridView中获取选定的DataRow?

我有一个绑定到DataGridView的DataTable。 我在DGV中启用了FullRowSelect。 有没有办法将所选行作为DataRow获取,以便我可以获得对所选行的值的强类型访问?

数据绑定到程序化创建的DataTable

假设我有一个像这样的数据表: DataTable dt = new DataTable(“Woot”); dt.Columns.AddRange(new DataColumn[]{ new DataColumn(“ID”,typeof(System.Guid)), new DataColumn(“Name”,typeof(String)) }); 当我尝试将控件绑定到它时: this.txtName.DataBindings.Add(“Text”, _dtRow, “Name”); 我得到这个例外: 无法绑定到DataSource上的属性或列名称。 参数名称:dataMember 知道为什么这个数据可以在dataAdapter创建的数据表上运行,但不能在程序创建的数据表上运行吗?

如果上一个param为null,则参数没有默认值?

我有这个查询似乎工作,除非我尝试在参数中传递空值: using (OleDbCommand com = new OleDbCommand(“INSERT INTO [GROUP] ([Group Number], [Group Name], [Address 1], ” + ” [Address 2], [City State Zip], [Phone], [Carrier Code], [Retail Plan Num 1], [Retail Plan Num 2], ” + ” [Retail Plan Num 3], [Retail Plan Num 4], [Retail Plan Num 5], [Plan Start Date 1], [Plan Start […]

从批处理SQL命令(SQL Server)检索“输出消息”

我正在针对SQL Server在一个批处理中执行几个离散查询。 例如: update tableX set colA =’freedom’; 从客户中选择lastName; 插入tableY(a,b,c)值(x,y,z); 现在,我想在DataSet中捕获结果(来自select语句),这很容易做到……但是我如何从该命令捕获“meta”响应,类似于Query Analyzer / SQL Mgt Studio的方式当它显示“消息”选项卡和diplays类似于: (1排受影响) (2行更新)