我想实现的功能是,数据库JCXT中事先有表Table_gjfirst,其中字段gjfirst_control是检查内容,其他字段值都为空,但用户在浏览器中需要根据gjfirst_control字段内容填写其他字段,就像在填表一样,最后将结果统一传回数据库。
datagrid表已经邦定,各个字段我都“编辑模板”,在itemtemplate中放入TEXT文本框,设它边框为无,则效果就和真正填表一样,只能看到单元格中的焦点了。
但是,所有单元格都填好后,我要怎样编写“提交”代码,才能将所有数据传给数据库呢?我在这里虚心讨教各位了,多谢!
下面是我的代码(缺提交代码):private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
{
SetBind();
}
} private void SetBind()
{
SqlConnection con=new SqlConnection("server=〈我的ip地址〉;database=JCXT;uid=sa;pwd=123;");
SqlDataAdapter da=new SqlDataAdapter("select * from Table_gjfirst",con);
DataSet ds=new DataSet();
da.Fill(ds,"table1");
this.DataGrid1.DataSource=ds.Tables["table1"];
this.DataGrid1.DataBind();
}
private void Button1_Click(object sender, System.EventArgs e)
{
//在此编写代码点击“提交”按钮后,每个单元格中的数据都传回数据库。不知如何写?
}
datagrid表已经邦定,各个字段我都“编辑模板”,在itemtemplate中放入TEXT文本框,设它边框为无,则效果就和真正填表一样,只能看到单元格中的焦点了。
但是,所有单元格都填好后,我要怎样编写“提交”代码,才能将所有数据传给数据库呢?我在这里虚心讨教各位了,多谢!
下面是我的代码(缺提交代码):private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
{
SetBind();
}
} private void SetBind()
{
SqlConnection con=new SqlConnection("server=〈我的ip地址〉;database=JCXT;uid=sa;pwd=123;");
SqlDataAdapter da=new SqlDataAdapter("select * from Table_gjfirst",con);
DataSet ds=new DataSet();
da.Fill(ds,"table1");
this.DataGrid1.DataSource=ds.Tables["table1"];
this.DataGrid1.DataBind();
}
private void Button1_Click(object sender, System.EventArgs e)
{
//在此编写代码点击“提交”按钮后,每个单元格中的数据都传回数据库。不知如何写?
}
SqlDataAdapter->DataSet->DataGrid
存数据:
DataGrid->DataSet->SqlDataAdapter(Update)
public DataSet CreateCmdsAndUpdate(DataSet myDataSet,string myConnection,string mySelectQuery,string myTableName)
{
OleDbConnection myConn = new OleDbConnection(myConnection);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter();
myDataAdapter.SelectCommand = new OleDbCommand(mySelectQuery, myConn);
OleDbCommandBuilder custCB = new OleDbCommandBuilder(myDataAdapter); myConn.Open(); DataSet custDS = new DataSet();
myDataAdapter.Fill(custDS); //code to modify data in dataset here myDataAdapter.Update(custDS, myTableName); myConn.Close(); return custDS;
}
1、使用SqlCommandBuilder(必须有主键)
myDataAdapter.SelectCommand = new SqlCommand(mySelectQuery, myConn);
SqlCommandBuilder cb = new SqlCommandBuilder(myDataAdapter);
//修改dataset里的数据
myDataAdapter.Update(ds, myTableName);2、写UpdateCommand
cmd = new SqlCommand("UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " +
"WHERE CustomerID = @oldCustomerID", conn); cmd.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
cmd.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName"); parm = cmd.Parameters.Add("@oldCustomerID", SqlDbType.NChar, 5, "CustomerID");
parm.SourceVersion = DataRowVersion.Original; da.UpdateCommand = cmd;这两个方法应该都可以,不好意思,没有按你的代码改。不难,试试,你可以的
private void Page_Load(object sender, System.EventArgs e)
{
ds = new DataSet();
if(!IsPostBack)
{
SetBind();
}
}
private void SetBind()
{
SqlConnection con=new SqlConnection("server=192.168.1.222;database=JCXT;uid=sa;pwd=123;");
SqlDataAdapter da=new SqlDataAdapter("select * from Table_gjfirst",con);
da.SelectCommand = new SqlCommand("select * from Table_gjfirst", con);
SqlCommandBuilder cb = new SqlCommandBuilder(da);
DataSet ds=new DataSet();
da.Fill(ds,"table1");
this.DataGrid1.DataSource=ds.Tables["table1"];
this.DataGrid1.DataBind();
}
private void btnUpdate_Click(object sender, System.EventArgs e)
{
if (ds.HasChanges())
{
foreach (DataGridItem di in DataGrid1.Items)
{ DataRow dr=ds.Tables["table1"].Rows.Find(DataGrid1.DataKeys[di.ItemIndex]);
dr["gjfirst_problem"]=((TextBox)di.FindControl("gjfirst_problem")).Text;
dr["gjfirst_findtime"]=((TextBox)di.FindControl("gjfirst_findtime")).Text;
dr["gjfirst_checkpeople"]=((TextBox)di.FindControl("gjfirst_people")).Text;
}
}
setBind();
}
}
}
错误提示:未将对象引用设置到对象的实例。private void SetBind()
{SqlConnection con=new SqlConnection("server=192.168.1.222;database=JCXT;uid=sa;pwd=123;");
SqlDataAdapter da=new SqlDataAdapter();
da.SelectCommand = new SqlCommand("select * from Table_gjfirst", con);
SqlCommandBuilder cb=new SqlCommandBuilder(da);
DataSet ds=new DataSet();
da.Fill(ds,"table1");
this.DataGrid1.DataSource=ds.Tables["table1"];
this.DataGrid1.DataBind();
}private void btnUpdate_Click(object sender, System.EventArgs e)
{
foreach (DataGridItem di in DataGrid1.Items)
{
SqlCommand = new SqlCommand("update Table_gjfirst set gjfirst_problem='"+((TextBox)di.FindControl("txt_problem")).Text+"',gjfirst_findtime='"+((TextBox)di.FindControl("txt_findtime")).Text+"',gjfirst_checkpeople='"+((TextBox)di.FindControl("txt_checkpeople")).Text+"' where gjfirst_control='"+((Label)di.FindControl("lab_control")).Text+"'");
da.UpdateCommand = SqlCommand;
SetBind();
}
}
不然是不会更新的~~你上边的方法,是不是有的对像没有找到啊
SqlCommand = new SqlCommand("update Table_gjfirst set gjfirst_problem='"+((TextBox)di.FindControl("txt_problem")).Text+"',gjfirst_findtime='"+((TextBox)di.FindControl("txt_findtime")).Text+"',gjfirst_checkpeople='"+((TextBox)di.FindControl("txt_checkpeople")).Text+"' where gjfirst_control='"+((Label)di.FindControl("lab_control")).Text+"'");
请给意见,多谢!
private void SetBind()
{SqlConnection con=new SqlConnection("server=192.168.1.222;database=JCXT;uid=sa;pwd=123;");
SqlDataAdapter da=new SqlDataAdapter();
da.SelectCommand = new SqlCommand("select * from Table_gjfirst", con);
SqlCommandBuilder cb=new SqlCommandBuilder(da);
DataSet ds=new DataSet();
da.Fill(ds,"table1");
this.DataGrid1.DataSource=ds.Tables["table1"];
this.DataGrid1.DataBind();
}private void btnUpdate_Click(object sender, System.EventArgs e)
{
da.Update(ds,"table1");
}错误提示为:Update 无法找到 TableMapping['table1'] 或 DataTable“table1”。
等待达人给出容易解决的答案!