//更新数据源的方法
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]
贴出来报什么错,或者程序怎么不正确。
strconn
这里连接字符串对不对?
http://blog.sina.com.cn/s/blog_7850ccad01014p76.html
注意,修改的时候不需要select,否则你重新加载更新了datagridview,那么你的修改就白费了。
//保存按钮:更新操作(主表和子表)
private void btnSave_Click(object sender, EventArgs e)
{
//int id = 0;
//保存到数据库之前先判断一下主表是否有重复记录
string fno = txtfNo.Text.Trim();
string fname = txtPname.Text.Trim();
int orderqty = Convert.ToInt32(txtOrderQty.Text);
double orderc = Convert.ToDouble(txtOrderCoefficient.Text);
Image pic = pbchef.Image;
//主表的修改
string sql1 = "select count(1) from desProductAttach where fno='" + fno + "' and fno!='" + args["pNo"].ToString() + "' and fname='" + fname + "' and orderqty=" + orderqty + " and orderCoefficient=" + orderc + "";
int count = Convert.ToInt32(SqlHelp.GetValue(CommandType.Text, sql1));
if (count != 0)
{
MessageBox.Show("数据库中已经存在重复记录,请修改后重新录入!");
return;
}
string sql2 = @"update desProductAttach set fno='" + fno + "',fname='" + fname + "',orderqty=" + orderqty + ",orderCoefficient=" + orderc + " where id="+id+"";
SqlHelp.ExecuteNonQuery(CommandType.Text,sql2);
//子表的更新(通过数据源更新)
//int flag = 2;
//flag= UpdateByDataSet(ds1, "product_desProductTime",strconn);
if (ds1.HasChanges())
{
SqlCommandBuilder cb = new SqlCommandBuilder(sda1);
sda1.Update(ds1.Tables[0]);
ds1.AcceptChanges();
dgvHead.Update();
}
}
private void dgvHead_CellLeave(object sender, DataGridViewCellEventArgs e)
{
//第一次加载时候,dgvHead.Rows[e.RowIndex].Cells[e.ColumnIndex].Value默认是第一行的id值
if (dgvHead.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == null)
{
tempCellValue = null;
return;
}
tempCellValue = dgvHead.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
}
//失去焦点 dgv
private void dgvHead_CellValidated(object sender, DataGridViewCellEventArgs e)
{
string curValue = dgvHead.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
if (tempCellValue==curValue)
{
return;
}
else
{
if (e.ColumnIndex==3)
{
string workTimeNo = dgvHead.CurrentRow.Cells[3].Value.ToString();
string sql2 = @"select count(*) from desProductTime where workTimeNo='"+workTimeNo+"'";
cn = new SqlConnection(strconn);
cmd = new SqlCommand(sql2,cn);
cn.Open();
int x = (int)(cmd.ExecuteScalar());
cn.Close();
if (x!=0)
{
cn = new SqlConnection(strconn);
string sql3 = @"select * from desProductTime where workTimeNo='"+workTimeNo+"'";
cmd = new SqlCommand(sql3,cn);
cn.Open();
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
dgvHead.Rows[e.RowIndex].Cells[1].Value = txtfNo.Text;
dgvHead.Rows[e.RowIndex].Cells[2].Value = "头部";
dgvHead.Rows[e.RowIndex].Cells[4].Value = sdr["initialCount"].ToString();
dgvHead.Rows[e.RowIndex].Cells[5].Value = sdr["seamDistance"].ToString();
dgvHead.Rows[e.RowIndex].Cells[6].Value = sdr["endCount"].ToString();
dgvHead.Rows[e.RowIndex].Cells[7].Value = (int)dgvHead.Rows[e.RowIndex].Cells[4].Value + (int)dgvHead.Rows[e.RowIndex].Cells[5].Value + (int)(dgvHead.Rows[e.RowIndex].Cells[6].Value);
dgvHead.Rows[e.RowIndex].Cells[8].Value = sdr["worktimeName"].ToString();
dgvHead.Rows[e.RowIndex].Cells[9].Value = sdr["samTime"].ToString();
dgvHead.Rows[e.RowIndex].Cells[10].Value = sdr["target"].ToString();
dgvHead.Rows[e.RowIndex].Cells[11].Value = sdr["equipment"].ToString();
dgvHead.Rows[e.RowIndex].Cells[12].Value = sdr["tool"].ToString();
dgvHead.Rows[e.RowIndex].Cells[13].Value = id;
}
cn.Close();
}
else
{
MessageBox.Show("工序代码输入有误!");
return;
}
}
else
{
dgvHead.Rows[e.RowIndex].Cells[7].Value = (int)dgvHead.Rows[e.RowIndex].Cells[4].Value + (int)dgvHead.Rows[e.RowIndex].Cells[5].Value + (int)(dgvHead.Rows[e.RowIndex].Cells[6].Value);
}
}
}
相关界面截图:
修改的问题吧,,,,