我在 c# 中有一个 datagridview,我用我想要的数据库中的任何表填充它,从文本框中获取表名。一切都很好,直到我尝试将我在 datagridview 中所做的更改提交到数据库。
button2 是我将用于更新的按钮。我已经搜索过了,显然更新适配器应该可以工作,但在这种情况下它不起作用,我不知道为什么。这是我的代码:
public partial class Form1 : Form
{
static string connstr = "DataSource=localhost;Database=sc2db;Trusted_Connection=True;";
SqlConnection conn = new SqlConnection(connstr);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
SqlDataAdapter adap = new SqlDataAdapter();
DataTable dt = new DataTable();
//BindingSource bs = new BindingSource();
SqlCommand comm = new SqlCommand("select * from " + textBox1.Text, conn);
adap.SelectCommand = comm;
dataGridView1.DataSource = dt;
adap.Fill(dt);
}
catch (Exception ex)
{
label1.Text = ex.Message;
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
SqlDataAdapter adap = new SqlDataAdapter();
DataTable dt = new DataTable();
BindingSource bs = new BindingSource();
adap.Update(dt);
}
catch (Exception ex)
{
label1.Text = ex.Message;
}
}
}