谁能解释为什么在下面的代码中使用了 SqlDataAdapter
?没有这个适配器,代码工作正常。
另外,为什么我们要使用DataAdapter
?请帮助我理解此 DataAdapter
用法。
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
private void button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection con = new SqlConnection("Data Source=.....\\SQLEXPRESS;Initial Catalog=......;Integrated Security=True");
con.Open();
SqlDataAdapter da =new SqlDataAdapter(); // Why use `SqlDataAdapter` here?
SqlCommand sc = new SqlCommand("insert into bhargavc values(" + textBox1.Text + "," + textBox2.Text + ");", con);
var o = sc.ExecuteNonQuery();
MessageBox.Show(o + "record to be inserted");
con.Close();
}
catch (Exception)
{
MessageBox.Show("error in the code");
}
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
最佳答案
数据适配器就像数据库和数据集之间的中介一样工作。但是,数据适配器不能存储数据。它只是将数据库中的数据提供给数据集。
例如:
水管用于将水从源头(井、池塘等)输送到目的地。但是,管道不是用来储存水的。同样,数据适配器(如水管)将数据从数据库发送到数据集。
这应该可以让您更清楚地了解数据适配器。
关于c# - 数据适配器有什么用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16467147/