1

我有一个带有数据网格的表单,其中填充了来自 sqlserver 数据库的数据。数据网格填充得很好,但我无法将用户所做的更改回发到 sql 数据库中的表。我的表单代码如下:

public partial class frmTimesheet : Form
{
    private DataTable tableTS = new DataTable();
    private SqlDataAdapter adapter = new SqlDataAdapter();
    private int currentTSID = 0;

    public frmTimesheet()
    {
        InitializeComponent();
    }

    private void frmTimesheet_Load(object sender, EventArgs e)
    {
        string strUser = cUser.currentUser;            
        cMyDate cD = new cMyDate(DateTime.Now.ToString());
        DateTime date = cD.GetDate();
        txtDate.Text = date.ToString();
        cboTSUser.DataSource = cUser.GetListOfUsers("active");
        cboTSUser.DisplayMember = "UserID";
        cboTSUser.Text = strUser;
        CheckForTimeSheet();
        PopulateTimeSheet();
    }

    private void CheckForTimeSheet()
    {
        string strUser = cboTSUser.Text;
        cMyDate cD = new cMyDate(txtDate.Text);
        DateTime date = cD.GetDate();
        int newTSID = cTimesheet.TimeSheetExists(strUser, date);
        if (newTSID != this.currentTSID)
        {
            tableTS.Clear();
            if (newTSID == 0)
            {
                MessageBox.Show("Create TimeSheet");
            }
            else
            {
                this.currentTSID = newTSID;
            }
        }
    }

    private void PopulateTimeSheet()
    {
        try
        {
            string sqlText = "SELECT EntryID, CaseNo, ChargeCode, StartTime, FinishTime, Units " +
                             "FROM tblTimesheetEntries " +
                             "WHERE TSID = " + this.currentTSID + ";";
            SqlConnection linkToDB = new SqlConnection(cConnectionString.BuildConnectionString());
            SqlCommand sqlCom = new SqlCommand(sqlText, linkToDB);
            SqlDataAdapter adapter = new SqlDataAdapter(sqlCom);
            adapter.SelectCommand = sqlCom;
            SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
            adapter.Fill(tableTS);
            dataTimesheet.DataSource = tableTS;                
        }
        catch (Exception eX)
        {
            string eM = "Error Populating Timesheet";
            cError err = new cError(eX, eM);
            MessageBox.Show(eM + Environment.NewLine + eX.Message);
        }         
    }

    private void txtDate_Leave(object sender, EventArgs e)
    {
        CheckForTimeSheet();
        PopulateTimeSheet();            
    }

    private void cboTSUser_DropDownClosed(object sender, EventArgs e)
    {
        CheckForTimeSheet(); 
        PopulateTimeSheet();
    }

    private void dataTimesheet_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        try
        {   
            adapter.Update(tableTS);
        }
        catch (Exception eX)
        {
            string eM = "Error on frmTimesheet, dataTimesheet_CellValueChanged";
            cError err = new cError(eX, eM);
            MessageBox.Show(eM + Environment.NewLine + eX.Message);
        }
    }
}

没有异常发生,当我逐步解决问题时,似乎是 SqlCommandBuilder 不基于我的 gien SELECT 命令构建 INSERT / UPDATE / DELETE 命令。

谁能看到我做错了什么?

我错过了什么?

4

2 回答 2

3

您需要设置UpdateCommand而不是SelectCommand更新:

 SqlDataAdapter adapter = new SqlDataAdapter();
 SqlCommandBuilder sqlBld = new SqlCommandBuilder(adapter)
 adapter.UpdateCommand = sqlBld.GetUpdateCommand() ;
于 2012-04-14T02:23:13.713 回答
0

这个问题很老,但需要更正@Anurag Ranjhan 提供的答案。

您无需编写以下行:

   adapter.UpdateCommand = sqlBld.GetUpdateCommand() ;

足以写:

   SqlDataAdapter adapter = new SqlDataAdapter();
   SqlCommandBuilder sqlBld = new SqlCommandBuilder(adapter)
   //remove the next line
   //adapter.UpdateCommand = sqlBld.GetUpdateCommand() ;

Sql update/insert/delete 命令是根据这一行自动生成的

 SqlCommandBuilder sqlBld = new SqlCommandBuilder(adapter)

您可以通过执行以下行找到生成的更新sql:

 sqlBld.GetUpdateCommand().CommandText;

请参阅示例如何使用 Visual C# .NET 中的 SqlDataAdapter 对象更新 SQL Server 数据库

OP所说的问题可以通过查看客户端在 SQl Server Profiler.

于 2018-03-19T19:33:30.800 回答