我一直在浏览互联网,我知道这个主题还有其他帖子,但到目前为止还没有成功。
这是我的代码,用于将数据加载到名为DataView的DataGridView:
public void loadThis()
{
try
{
using (var conn = new SqlConnection(connection))
{
string sql = "SELECT * FROM Dates";
using (var cmd = new SqlCommand(sql, conn))
{
SqlDataAdapter DSA = new SqlDataAdapter(sql, conn);
SqlCommandBuilder builder = new SqlCommandBuilder(Sda);
conn.Open();
dt.Load(cmd.ExecuteReader());
DataView.DataSource = dt;
}
}
}
catch (SqlException ex)
{
MessageBox.Show(ex.ToString());
}
}
以下是我必须尝试将其保存回DataBase的内容:
private void btnSave_Click(object sender, EventArgs e)
{
using (var conn = new SqlConnection(connection))
{
try
{
Sda = new SqlDataAdapter("SELECT * FROM Dates", conn);
dt.AcceptChanges();
Sda.Update(dt);
SqlCommandBuilder builder = new SqlCommandBuilder(Sda);
builder.ConflictOption = ConflictOption.OverwriteChanges;
Sda.UpdateCommand = builder.GetUpdateCommand();//Sql get update command needs to be initializwed
Sda.Update(dt);
}
catch (SqlException ex)
{
MessageBox.Show(ex.ToString());
}
}
}
加载数据工作正常但是保存ammendmenta的当前代码会引发异常:
System.InvalidOperationException:'动态SQL生成 对于没有的SelectCommand,不支持UpdateCommand 返回任何关键列信息。'
谢谢大家!