C# 在C中处理数据库混淆
我正在尝试学习如何使用C语言处理数据库,并且在一个教程中,当我必须使用DataSet、SqlDataAdapter和SqlCommandBuilder时,我学习了其中的一部分。这是我在教程示例中编写的代码:C# 在C中处理数据库混淆,c#,ado.net,C#,Ado.net,我正在尝试学习如何使用C语言处理数据库,并且在一个教程中,当我必须使用DataSet、SqlDataAdapter和SqlCommandBuilder时,我学习了其中的一部分。这是我在教程示例中编写的代码: public void InitData() { //instantiate the connection conn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=\"D:\\
public void InitData() {
//instantiate the connection
conn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=\"D:\\Projects IDE\\Visual Studio\\Exercitii\\Console.app\\WindowsFormsApplication1\\WindowsFormsApplication1\\PlanetWrox.mdf\";Integrated Security=True;User Instance=True");
//1.instantiate a new DataSet
dsCustomers = new DataSet();
//2.init SqlDataAdapter with select command and connection
daCustomers = new SqlDataAdapter("SELECT Id ,Name, SortOrder FROM Genre", conn);
// 3. fill in insert, update, and delete commands
SqlCommandBuilder cmdBldr = new SqlCommandBuilder(daCustomers);
// 4. fill the dataset
daCustomers.Fill(dsCustomers, tableName);
}
public void btnUpdateClicked(object sender, EventArgs e) {
// write changes back to DataBase
daCustomers.Update(dsCustomers, tableName);
}
这里有几件事我不明白:
我注意到的第一件事是,我不必打开和关闭数据库。从我对数据库的有限知识中,我知道为了访问数据,你必须打开与它的连接,完成后你必须关闭它。这必须发生在幕后的某个地方。对吗?如果是这样的话,巫婆会这么做吗
第二个问题是关于SqlDataAdapter和SqlCommandBuilder的。我不明白SqlCommandBuilder在这里做什么。SqlDataAdapter不是执行sql查询的那个吗 dsCustomers.Fill为您打开和关闭连接。
SqlCommandBuilder根据select语句创建insert、update和delete。dsCustomers.Fill为您打开和关闭连接。
SqlCommandBuilder根据select语句创建insert、update和delete。ADO.NET在用数据填充表格时为您处理该任务。ADO.NET在用数据填充表格时为您处理该任务 我注意到的第一件事是,我不必打开和关闭 数据库 将为您打开/关闭连接 如果IDbConnection在调用Fill之前关闭,则会将其打开到 检索数据,然后关闭。如果加注前连接已打开 被称为,它仍然是开放的 从提供的select语句的元数据生成INSERT、UPDATE或DELETE语句。通过这种方式,您可以调用,对数据集所做的所有更改都将在数据库中自动更新
//instantiate the connection
using (SqlConnection conn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=\"D:\\Projects IDE\\Visual Studio\\Exercitii\\Console.app\\WindowsFormsApplication1\\WindowsFormsApplication1\\PlanetWrox.mdf\";Integrated Security=True;User Instance=True"))
{
//1.instantiate a new DataSet
dsCustomers = new DataSet();
//2.init SqlDataAdapter with select command and connection
daCustomers = new SqlDataAdapter("SELECT Id ,Name, SortOrder FROM Genre", conn);
// 3. fill in insert, update, and delete commands
SqlCommandBuilder cmdBldr = new SqlCommandBuilder(daCustomers);
// 4. fill the dataset
daCustomers.Fill(dsCustomers, tableName);
}
我注意到的第一件事是,我不必打开和关闭
数据库
将为您打开/关闭连接
如果IDbConnection在调用Fill之前关闭,则会将其打开到
检索数据,然后关闭。如果加注前连接已打开
被称为,它仍然是开放的
从提供的select语句的元数据生成INSERT、UPDATE或DELETE语句。通过这种方式,您可以调用,对数据集所做的所有更改都将在数据库中自动更新
//instantiate the connection
using (SqlConnection conn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=\"D:\\Projects IDE\\Visual Studio\\Exercitii\\Console.app\\WindowsFormsApplication1\\WindowsFormsApplication1\\PlanetWrox.mdf\";Integrated Security=True;User Instance=True"))
{
//1.instantiate a new DataSet
dsCustomers = new DataSet();
//2.init SqlDataAdapter with select command and connection
daCustomers = new SqlDataAdapter("SELECT Id ,Name, SortOrder FROM Genre", conn);
// 3. fill in insert, update, and delete commands
SqlCommandBuilder cmdBldr = new SqlCommandBuilder(daCustomers);
// 4. fill the dataset
daCustomers.Fill(dsCustomers, tableName);
}
这将自动关闭连接并进行处置,而无需为此烦恼
这将自动关闭连接并进行处置,而您无需为此操心。Fill将锁定进程中涉及的所有资源。
此外,根据sql设置,它也可能会阻止其他资源
如果您将此代码用于某些实时工作,请仅在需要时选择。Fill将锁定此过程中涉及的所有资源。
此外,根据sql设置,它也可能会阻止其他资源
如果您将此代码用于某些实时工作,请仅在需要时选择此代码。首先:
我们使用conn.open;打开连接并关闭连接;关闭连接
在您的程序中,您并没有像连接sqlserver那样连接到数据库。您已经提供了显示的文件物理路径
第二件事见下文:
首先:
我们使用conn.open;打开连接并关闭连接;关闭连接
在您的程序中,您并没有像连接sqlserver那样连接到数据库。您已经提供了显示的文件物理路径
第二件事见下文:
当你打电话给客户时,填写。。当您调用daCustomers.Fill.时,ADO.NET会为您打开和关闭与数据库的连接。。ADO.NET为您打开和关闭与数据库的连接