我需要将 datagridview 中的某些列更新到数据库。但不要更新到数据库。
第一步:我从 datetimepicker 中选择 datetime。
第二步:在 datagridview 上显示日期时间。
步骤树:我需要更新/编辑 datagridview 到数据库。
在 Datagridview 上显示。
EmpNo     fName     ChkDate     ChkIn   ChkOut
00001     Al        01/10/2012  08:02   17:04
00002     Bik       01/10/2012  07:43   18:35
我需要将字段“ChkIn”更新到数据库。
代码
SqlConnection Conn;
SqlCommand cmd = new SqlCommand();
SqlDataAdapter da;
DataTable dt = new DataTable();
DataSet ds = new DataSet();
StringBuilder sb = new StringBuilder();
string appConn = ConfigurationManager.ConnectionStrings["connDB"].ConnectionString;
int i;
    for (i = 1; i < dgvShow.Rows.Count; i++)
    {
        if (dgvShow.Rows.Count > 0)
        {
            SqlConnection conn = new SqlConnection(appConn);
            string sql = "UPDATE [WebSP].[dbo].[filesTA]"
            + "SET [filesTA].ChkIn = replace(convert(nvarchar(10),'" + dgvShow.Rows[i].Cells[3].Value + "',102),'.',':')"
            + "FROM [WebSP].[dbo].[filesTA]"
            + "WHERE [filesTA].ChkDate = '" + dateTimePicker.Value.ToString("yyyy-MM-dd") + "' and [filesTA].EmpNo = '" + dgvShow.Rows[i].Cells[0].Value + "'";
            da = new SqlDataAdapter(sql, Conn);
            DataSet ds = new DataSet();
            da.Fill(ds);
            Conn.Close();
            dgvShow.DataSource = ds;
            da.Update(ds);
        }
    }
错误:更新无法找到 TableMapping['Table'] 或 DataTable 'Table'。
我尝试其他代码:
Conn = new SqlConnection();
            if (Conn.State == ConnectionState.Open)
            {
                Conn.Close();
            }
            Conn.ConnectionString = appConn;
            Conn.Open();
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM [filesTA]", appConn);
            adapter.UpdateCommand = new SqlCommand("UPDATE [WebSP].[dbo].[filesTA]"
            + "SET [filesTA].ChkIn = replace(convert(nvarchar(10),@cIn,102),'.',':')"
            + "FROM [WebSP].[dbo].[filesTA]"
            + "WHERE [filesTA].ChkDate = @cDate and [filesTA].EmpNo = @eNo", Conn);
            adapter.UpdateCommand.Parameters.Add("@cIn", SqlDbType.NVarChar, 10, "ChkIn");
            adapter.UpdateCommand.Parameters.Add("@cDate", SqlDbType.NVarChar, 10, "ChkDate");
            adapter.UpdateCommand.Parameters.Add("@eNo", SqlDbType.NVarChar, 10, "EmpNo");
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            dgvShow.DataSource = ds;
            adapter.Update(ds);
此代码不保存到数据库。
谢谢你的时间。:D
类型数据库:
ChkIn 和 ChkDate 类型 DateTime,EmpNo 类型 NUMERIC
我试试
int i;
            for (i = 1; i < dgvShow.Rows.Count; i++)
            {
                if (dgvShow.Rows.Count > 0)
                {
                    using (Conn = new SqlConnection(appConn))
                    {
                        Conn.Open();
                        string sql = "UPDATE [WebSP].[dbo].[filesTA]" +
                                     "SET [filesTA].ChkIn = replace(convert(nvarchar(10),@cIn,102),'.',':')" +
                                     "FROM [WebSP].[dbo].[filesTA]" +
                                     "WHERE [filesTA].ChkDate = @cDate and [filesTA].EmpNo = @eNo";
                        SqlCommand cmd = new SqlCommand(sql, Conn);
                        cmd.Parameters.Add("@cIn", SqlDbType.DateTime, 10, "ChkIn").Value = Convert.ToDateTime(dgvShow.Rows[i].Cells[3].Value).ToString();
                        cmd.Parameters.Add("@cDate", SqlDbType.DateTime, 10, "ChkDate").Value = Convert.ToDateTime(dateTimePicker.Value.ToString()).ToString();
                        cmd.Parameters.Add("@eNo", SqlDbType.Decimal, 10, "EmpNo").Value = Convert.ToDecimal(dgvShow.Rows[i].Cells[0].Value).ToString();
                        cmd.ExecuteNonQuery();
                    }
                }
            }
错误:从字符串转换日期和/或时间时转换失败。T__T