//更新数据源的方法
private int UpdateByDataSet(DataSet ds, string strTblName, string strconn)
{
// dgvHead.Columns["pid"].Visible = true;
//dgvHead.Columns["fNo"].Visible = true;
//dgvHead.Columns["positation"].Visible = true;
try
{
//dgvHead.AllowUserToAddRows = false;
SqlConnection cn = new SqlConnection(strconn);
SqlDataAdapter sda0 = new SqlDataAdapter();
//sql是要更新的源表中的部分
string sql = @"select id,
worktimeNo as 工序代码,
initialCount as 起始针数,
seamDistance as 车缝距离,
endCount as 落回针数,
worktimeName as 工序名称,
samTime as SAM值,
target AS 工序指标,
equipment as 使用设备,
tool as 使用工具,
pid from " + strTblName + " where pid=" + id + " and positation='头部' and fno='" + txtfNo.Text + "'";
SqlCommand cmd1 = new SqlCommand(sql, cn);
sda0.SelectCommand = cmd1;
//sda.UpdateCommand = cmd;
//sda.DeleteCommand = cmd;
//sda.InsertCommand = cmd;
//sda1.Fill(ds, strTblName);
SqlCommandBuilder cb = new SqlCommandBuilder(sda0);
sda0.Update(ds, strTblName);
return 0;
}
catch (Exception ee)
{
MessageBox.Show(ee.ToString());
return -1;
}
}
datagridview初始化
private void dgvHeadInit(int id)
{
string sql1 = @"select id,
worktimeNo as 工序代码,
initialCount as 起始针数,
seamDistance as 车缝距离,
endCount as 落回针数,
initialCount+seamDistance+endCount as [每小时指标(件)],
worktimeName as 工序名称,
samTime as SAM值,
target AS 工序指标,
equipment as 使用设备,
tool as 使用工具,
pid
from product_desProductTime
where pid=" + id + " and positation='头部' and fno='"+txtfNo.Text+"'";
SqlConnection cn = new SqlConnection("server=172.20.50.4;uid=sa;pwd=p4japo020;database=gsd");
sda1 = new SqlDataAdapter(sql1, cn);
ds1 = new DataSet();
sda1.Fill(ds1);
dt1 = ds1.Tables[0];
ds1.Tables[0].TableName = "product_desProductTime";
dgvHead.DataSource = dt1;
dgvHead.Columns["id"].Visible = false;
}
private void btnSave_Click(object sender, EventArgs e)
{
int flag = 2;
flag= UpdateByDataSet(ds1, "product_desProductTime",strconn);
}
我的初衷是通过直接在ui层操作数据(增删改),然后点击保存按钮,数据就会更新到数据库表中;我自己写了一个测试程序,功能完全可以实现,但是移植到项目中就老是不行,求解释,我都纠结一整天了 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!要发狂了
补充一下,数据库表结构:
CREATE TABLE [dbo].product_desProductTime NOT NULL,
[fNo] nvarchar NULL,
[positation] nvarchar NULL,
[worktimeNo] nvarchar NULL,
[worktimeName] nvarchar NULL,
[samTime] decimal NULL,
[target] decimal NULL,
[equipment] nvarchar NULL,
[tool] nvarchar NULL,
[initialCount] [int] NULL,
[seamDistance] [int] NULL,
[endCount] [int] NULL,
[pid] [int] NULL,
CONSTRAINT [PK__product___3213E83F123EB7A3] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]