SqlDataAdapter sda;
DataSet ds;
bool ss=false;
protected void Page_Load(object sender, EventArgs e)
{
if (ss== false)
{
SqlConnection conn = new SqlConnection(@"Data Source=PC-200907241220\SQLEXPRESS;Initial Catalog=ReSources;Integrated Security=True");
conn.Open();
dgvMain.DataSource = SelectDataTable();
this.DataBind();
ss= true;
}
}
//查询表
public DataTable SelectDataTable()
{
SqlCommand cmd = new SqlCommand("select * from Table1", conn);
sda = new SqlDataAdapter(cmd);
ds = new DataSet();
sda.Fill(ds);
return ds.Tables[0];
}
//添加
protected void btnAdd_Click(object sender, EventArgs e)
{
DataRow sqlDr = ds.Tables[0].NewRow();
sqlDr["列1"] = "123";
sqlDr["列2"] = "123";
sqlDr["列3"] = "123";
ds.Tables[0].Rows.Add(sqlDr);
// this.DataBind();
}
//保存
protected void btnSave_Click(object sender, EventArgs e)
{
SqlCmd();
sda.Update(ds);
dgvMain.DataSource = SelectDataTable();
this.DataBind();
}
//命令
private void SqlCmd()
{
String inSertSql = "insert into Table1(列1,列2,列3) values(@a,@b,@c) "; //插入命令
SqlCommand insertCommand = new SqlCommand(inSertSql, conn);
insertCommand.Parameters.Add("@a", SqlDbType.VarChar, 36, "列1");
insertCommand.Parameters.Add("@b", SqlDbType.VarChar, 20, "列2");
insertCommand.Parameters.Add("@c", SqlDbType.VarChar, 20, "列3");
sda.InsertCommand = insertCommand;
}
按添加按钮已经能发数据添加进GridView了,但是按保存没有添加进数据库
winfrom中就没错, .net就没效果,有谁知道的帮忙下看下
DataSet ds;
bool ss=false;
protected void Page_Load(object sender, EventArgs e)
{
if (ss== false)
{
SqlConnection conn = new SqlConnection(@"Data Source=PC-200907241220\SQLEXPRESS;Initial Catalog=ReSources;Integrated Security=True");
conn.Open();
dgvMain.DataSource = SelectDataTable();
this.DataBind();
ss= true;
}
}
//查询表
public DataTable SelectDataTable()
{
SqlCommand cmd = new SqlCommand("select * from Table1", conn);
sda = new SqlDataAdapter(cmd);
ds = new DataSet();
sda.Fill(ds);
return ds.Tables[0];
}
//添加
protected void btnAdd_Click(object sender, EventArgs e)
{
DataRow sqlDr = ds.Tables[0].NewRow();
sqlDr["列1"] = "123";
sqlDr["列2"] = "123";
sqlDr["列3"] = "123";
ds.Tables[0].Rows.Add(sqlDr);
// this.DataBind();
}
//保存
protected void btnSave_Click(object sender, EventArgs e)
{
SqlCmd();
sda.Update(ds);
dgvMain.DataSource = SelectDataTable();
this.DataBind();
}
//命令
private void SqlCmd()
{
String inSertSql = "insert into Table1(列1,列2,列3) values(@a,@b,@c) "; //插入命令
SqlCommand insertCommand = new SqlCommand(inSertSql, conn);
insertCommand.Parameters.Add("@a", SqlDbType.VarChar, 36, "列1");
insertCommand.Parameters.Add("@b", SqlDbType.VarChar, 20, "列2");
insertCommand.Parameters.Add("@c", SqlDbType.VarChar, 20, "列3");
sda.InsertCommand = insertCommand;
}
按添加按钮已经能发数据添加进GridView了,但是按保存没有添加进数据库
winfrom中就没错, .net就没效果,有谁知道的帮忙下看下
ds = new DataSet();
da = new SqlDataAdapter(SQL语句, cn);
cb = new SqlCommandBuilder(da);
da.Fill(ds);
//更改ds中的值
da.Update(ds);
//搞掂
上面代码移到winform一点问题都没有,就是在.net中不行,我是新学.net ,不懂为什么添加不了
DataSet ds;
bool ss=false;
protected void Page_Load(object sender, EventArgs e)
{
if (ss== false)
{
SqlConnection conn = new SqlConnection(@"Data Source=PC-200907241220\SQLEXPRESS;Initial Catalog=ReSources;Integrated Security=True");
这里最好判断下Conn的状态 :if(conn.State !=ConnectiongState.Open){//DO IT}
conn.Open();
dgvMain.DataSource = SelectDataTable();
this.DataBind();
ss= true;
}
}
//查询表
public DataTable SelectDataTable()
{
SqlCommand cmd = new SqlCommand("select * from Table1", conn);确认这里的conn就是你上面已经open的那个connection sda = new SqlDataAdapter(cmd);
ds = new DataSet();
sda.Fill(ds);
return ds.Tables[0];
}
//添加
protected void btnAdd_Click(object sender, EventArgs e)
{
DataRow sqlDr = ds.Tables[0].NewRow();
sqlDr["列1"] = "123";
sqlDr["列2"] = "123";
sqlDr["列3"] = "123";
ds.Tables[0].Rows.Add(sqlDr);
// this.DataBind();
}
//保存
protected void btnSave_Click(object sender, EventArgs e)
{
SqlCmd();
sda.Update(ds);
dgvMain.DataSource = SelectDataTable();
this.DataBind();
}
//命令
private void SqlCmd()
{
String inSertSql = "insert into Table1(列1,列2,列3) values(@a,@b,@c) "; //插入命令
SqlCommand insertCommand = new SqlCommand(inSertSql, conn);
insertCommand.Parameters.Add("@a", SqlDbType.VarChar, 36, "列1");
insertCommand.Parameters.Add("@b", SqlDbType.VarChar, 20, "列2");
insertCommand.Parameters.Add("@c", SqlDbType.VarChar, 20, "列3");
sda.InsertCommand = insertCommand;
}你的代码不完整,如果真解决不了发一分到我邮箱,帮你改完在回邮给你: [email protected]
{
bind();
}
在page_load里