private void button1_Click(object sender, EventArgs e)
{
string connectString = this.txtConn.Text.Trim();//数据库连接字符串,用一个字符窜来接受txt中的文本
SqlConnection con = new System.Data.SqlClient.SqlConnection(connectString);//创建一个连接对象
con.Open();//打开连接
SqlCommand sqlCmd = new System.Data.SqlClient.SqlCommand();//创建一个Command对象
sqlCmd.Connection = con;
sqlCmd.CommandText = "SELECT * FROM booktable";//sql语句
System.Data.SqlClient.SqlDataAdapter sqlAdapter = new System.Data.SqlClient.SqlDataAdapter(sqlCmd);
DataTable table = new DataTable();
sqlAdapter.Fill(table);//填充数据到表格;
this.dataGridView1.DataSource = table;//给GridView控制数据源赋值
con.Close();//(联接结束)
}
5 个解决方案
#1
insert
update
or
dataapter
update
or
dataapter
#2
private void toolDelete_Click(object sender, EventArgs e)
{
if (MessageBox.Show("确定删除?", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
{
string sql = "DELETE FROM SYSRULE WHERE ID_RULE=@ID_RULE";
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.CommandText = sql;
sqlCmd.Parameters.Add(new SqlParameter("@ID_RULE", this.dtRule.Rows[bind.Position]["ID_RULE"]));
this.dataAccess.ExecuteSql(sqlCmd);
this.dtRule.Rows.RemoveAt(bind.Position);
}
}
================================================
if (CheckData())
{
MessageBox.Show("数据检查失败!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
SqlCommand sqlCmd = new SqlCommand();
string sql;
if (FormStatus == Status.AddStatus)
{
sql = "INSERT INTO SYSRULE(NO_RULE,NM_RULE,ID_EMP,RM)VALUES(@RULENO,@RULENAME,@EMPID,@RM)";
sqlCmd.CommandText = sql;
sqlCmd.Parameters.Add(new SqlParameter("@RULENO", this.txtRuleNo.Text.ToString()));
MessageBox.Show(this.txtRuleName.Text.ToString());
sqlCmd.Parameters.Add(new SqlParameter("@RULENAME", this.txtRuleName.Text.ToString()));
sqlCmd.Parameters.Add(new SqlParameter("@EMPID", idEmp));
sqlCmd.Parameters.Add(new SqlParameter("@RM", this.txtRm.Text.ToString()));
this.dataAccess.ExecuteSql(sqlCmd);
}
else if (FormStatus == Status.ModifySatus)
{
sql = "UPDATE SYSRULE SET NO_RULE=@RULENO,NM_RULE=@RULENAME,ID_EMP=@EMPID,RM=@RM WHERE ID_RULE=@RULEID";
sqlCmd.CommandText = sql;
sqlCmd.Parameters.Add(new SqlParameter("@RULEID", this.dtRule.Rows[bind.Position]["ID_RULE"]));
sqlCmd.Parameters.Add(new SqlParameter("@RULENO", this.txtRuleNo.Text.ToString()));
sqlCmd.Parameters.Add(new SqlParameter("@RULENAME", this.txtRuleName.Text.ToString()));
sqlCmd.Parameters.Add(new SqlParameter("@EMPID", idEmp));
sqlCmd.Parameters.Add(new SqlParameter("@RM", this.txtRm.Text.ToString()));
this.dataAccess.ExecuteSql(sqlCmd);
}
================================dataAccess.cs的一部分
#region 执行SQL指令
public void ExecuteSql(SqlCommand sqlCmd)
{
//throw new System.NotImplementedException();
sqlCmd.Connection = this.cnn;
try
{
cnn.Open();
sqlCmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new Exception("数据更新出错!", ex);
}
finally
{
cnn.Close();
}
}
#endregion
{
if (MessageBox.Show("确定删除?", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
{
string sql = "DELETE FROM SYSRULE WHERE ID_RULE=@ID_RULE";
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.CommandText = sql;
sqlCmd.Parameters.Add(new SqlParameter("@ID_RULE", this.dtRule.Rows[bind.Position]["ID_RULE"]));
this.dataAccess.ExecuteSql(sqlCmd);
this.dtRule.Rows.RemoveAt(bind.Position);
}
}
================================================
if (CheckData())
{
MessageBox.Show("数据检查失败!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
SqlCommand sqlCmd = new SqlCommand();
string sql;
if (FormStatus == Status.AddStatus)
{
sql = "INSERT INTO SYSRULE(NO_RULE,NM_RULE,ID_EMP,RM)VALUES(@RULENO,@RULENAME,@EMPID,@RM)";
sqlCmd.CommandText = sql;
sqlCmd.Parameters.Add(new SqlParameter("@RULENO", this.txtRuleNo.Text.ToString()));
MessageBox.Show(this.txtRuleName.Text.ToString());
sqlCmd.Parameters.Add(new SqlParameter("@RULENAME", this.txtRuleName.Text.ToString()));
sqlCmd.Parameters.Add(new SqlParameter("@EMPID", idEmp));
sqlCmd.Parameters.Add(new SqlParameter("@RM", this.txtRm.Text.ToString()));
this.dataAccess.ExecuteSql(sqlCmd);
}
else if (FormStatus == Status.ModifySatus)
{
sql = "UPDATE SYSRULE SET NO_RULE=@RULENO,NM_RULE=@RULENAME,ID_EMP=@EMPID,RM=@RM WHERE ID_RULE=@RULEID";
sqlCmd.CommandText = sql;
sqlCmd.Parameters.Add(new SqlParameter("@RULEID", this.dtRule.Rows[bind.Position]["ID_RULE"]));
sqlCmd.Parameters.Add(new SqlParameter("@RULENO", this.txtRuleNo.Text.ToString()));
sqlCmd.Parameters.Add(new SqlParameter("@RULENAME", this.txtRuleName.Text.ToString()));
sqlCmd.Parameters.Add(new SqlParameter("@EMPID", idEmp));
sqlCmd.Parameters.Add(new SqlParameter("@RM", this.txtRm.Text.ToString()));
this.dataAccess.ExecuteSql(sqlCmd);
}
================================dataAccess.cs的一部分
#region 执行SQL指令
public void ExecuteSql(SqlCommand sqlCmd)
{
//throw new System.NotImplementedException();
sqlCmd.Connection = this.cnn;
try
{
cnn.Open();
sqlCmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new Exception("数据更新出错!", ex);
}
finally
{
cnn.Close();
}
}
#endregion
#3
对数据库的操作:包括创建数据库、创建表、创建存贮过程、创建视图、删除表、删除存贮过程、删除视图、读表数据、更新整个表数据等等,非常详细。
摘录:《程序员秘书》--源代码--数据库--SQL数据库各种操作
详见:http;//www.psec.net.cn
摘录:《程序员秘书》--源代码--数据库--SQL数据库各种操作
详见:http;//www.psec.net.cn
#4
string connectString = this.txtConn.Text.Trim();//数据库连接字符串,用一个字符窜来接受txt中的文本
using (SqlConnection con = new System.Data.SqlClient.SqlConnection(connectString))
{
con.Open();//打开连接
SqlCommand sqlCmd = new System.Data.SqlClient.SqlCommand();//创建一个Command对象
sqlCmd.Connection = con;
sqlCmd.CommandText = "UPDATE…… or DELETE……";//sql语句
sqlCmd.ExecuteNonQuery();
con.Close();//(联接结束)
}
using (SqlConnection con = new System.Data.SqlClient.SqlConnection(connectString))
{
con.Open();//打开连接
SqlCommand sqlCmd = new System.Data.SqlClient.SqlCommand();//创建一个Command对象
sqlCmd.Connection = con;
sqlCmd.CommandText = "UPDATE…… or DELETE……";//sql语句
sqlCmd.ExecuteNonQuery();
con.Close();//(联接结束)
}
#5
ADO.NET中的多数据表操作浅析
http://blog.csdn.net/zhzuo/archive/2004/08/06/67016.aspx
http://blog.csdn.net/zhzuo/archive/2004/08/06/67037.aspx
透过vs.net数据窗体向导看Ado.net
http://blog.csdn.net/zhzuo/archive/2005/01/03/238273.aspx
http://blog.csdn.net/zhzuo/archive/2004/08/06/67016.aspx
http://blog.csdn.net/zhzuo/archive/2004/08/06/67037.aspx
透过vs.net数据窗体向导看Ado.net
http://blog.csdn.net/zhzuo/archive/2005/01/03/238273.aspx
#1
insert
update
or
dataapter
update
or
dataapter
#2
private void toolDelete_Click(object sender, EventArgs e)
{
if (MessageBox.Show("确定删除?", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
{
string sql = "DELETE FROM SYSRULE WHERE ID_RULE=@ID_RULE";
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.CommandText = sql;
sqlCmd.Parameters.Add(new SqlParameter("@ID_RULE", this.dtRule.Rows[bind.Position]["ID_RULE"]));
this.dataAccess.ExecuteSql(sqlCmd);
this.dtRule.Rows.RemoveAt(bind.Position);
}
}
================================================
if (CheckData())
{
MessageBox.Show("数据检查失败!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
SqlCommand sqlCmd = new SqlCommand();
string sql;
if (FormStatus == Status.AddStatus)
{
sql = "INSERT INTO SYSRULE(NO_RULE,NM_RULE,ID_EMP,RM)VALUES(@RULENO,@RULENAME,@EMPID,@RM)";
sqlCmd.CommandText = sql;
sqlCmd.Parameters.Add(new SqlParameter("@RULENO", this.txtRuleNo.Text.ToString()));
MessageBox.Show(this.txtRuleName.Text.ToString());
sqlCmd.Parameters.Add(new SqlParameter("@RULENAME", this.txtRuleName.Text.ToString()));
sqlCmd.Parameters.Add(new SqlParameter("@EMPID", idEmp));
sqlCmd.Parameters.Add(new SqlParameter("@RM", this.txtRm.Text.ToString()));
this.dataAccess.ExecuteSql(sqlCmd);
}
else if (FormStatus == Status.ModifySatus)
{
sql = "UPDATE SYSRULE SET NO_RULE=@RULENO,NM_RULE=@RULENAME,ID_EMP=@EMPID,RM=@RM WHERE ID_RULE=@RULEID";
sqlCmd.CommandText = sql;
sqlCmd.Parameters.Add(new SqlParameter("@RULEID", this.dtRule.Rows[bind.Position]["ID_RULE"]));
sqlCmd.Parameters.Add(new SqlParameter("@RULENO", this.txtRuleNo.Text.ToString()));
sqlCmd.Parameters.Add(new SqlParameter("@RULENAME", this.txtRuleName.Text.ToString()));
sqlCmd.Parameters.Add(new SqlParameter("@EMPID", idEmp));
sqlCmd.Parameters.Add(new SqlParameter("@RM", this.txtRm.Text.ToString()));
this.dataAccess.ExecuteSql(sqlCmd);
}
================================dataAccess.cs的一部分
#region 执行SQL指令
public void ExecuteSql(SqlCommand sqlCmd)
{
//throw new System.NotImplementedException();
sqlCmd.Connection = this.cnn;
try
{
cnn.Open();
sqlCmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new Exception("数据更新出错!", ex);
}
finally
{
cnn.Close();
}
}
#endregion
{
if (MessageBox.Show("确定删除?", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
{
string sql = "DELETE FROM SYSRULE WHERE ID_RULE=@ID_RULE";
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.CommandText = sql;
sqlCmd.Parameters.Add(new SqlParameter("@ID_RULE", this.dtRule.Rows[bind.Position]["ID_RULE"]));
this.dataAccess.ExecuteSql(sqlCmd);
this.dtRule.Rows.RemoveAt(bind.Position);
}
}
================================================
if (CheckData())
{
MessageBox.Show("数据检查失败!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
SqlCommand sqlCmd = new SqlCommand();
string sql;
if (FormStatus == Status.AddStatus)
{
sql = "INSERT INTO SYSRULE(NO_RULE,NM_RULE,ID_EMP,RM)VALUES(@RULENO,@RULENAME,@EMPID,@RM)";
sqlCmd.CommandText = sql;
sqlCmd.Parameters.Add(new SqlParameter("@RULENO", this.txtRuleNo.Text.ToString()));
MessageBox.Show(this.txtRuleName.Text.ToString());
sqlCmd.Parameters.Add(new SqlParameter("@RULENAME", this.txtRuleName.Text.ToString()));
sqlCmd.Parameters.Add(new SqlParameter("@EMPID", idEmp));
sqlCmd.Parameters.Add(new SqlParameter("@RM", this.txtRm.Text.ToString()));
this.dataAccess.ExecuteSql(sqlCmd);
}
else if (FormStatus == Status.ModifySatus)
{
sql = "UPDATE SYSRULE SET NO_RULE=@RULENO,NM_RULE=@RULENAME,ID_EMP=@EMPID,RM=@RM WHERE ID_RULE=@RULEID";
sqlCmd.CommandText = sql;
sqlCmd.Parameters.Add(new SqlParameter("@RULEID", this.dtRule.Rows[bind.Position]["ID_RULE"]));
sqlCmd.Parameters.Add(new SqlParameter("@RULENO", this.txtRuleNo.Text.ToString()));
sqlCmd.Parameters.Add(new SqlParameter("@RULENAME", this.txtRuleName.Text.ToString()));
sqlCmd.Parameters.Add(new SqlParameter("@EMPID", idEmp));
sqlCmd.Parameters.Add(new SqlParameter("@RM", this.txtRm.Text.ToString()));
this.dataAccess.ExecuteSql(sqlCmd);
}
================================dataAccess.cs的一部分
#region 执行SQL指令
public void ExecuteSql(SqlCommand sqlCmd)
{
//throw new System.NotImplementedException();
sqlCmd.Connection = this.cnn;
try
{
cnn.Open();
sqlCmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new Exception("数据更新出错!", ex);
}
finally
{
cnn.Close();
}
}
#endregion
#3
对数据库的操作:包括创建数据库、创建表、创建存贮过程、创建视图、删除表、删除存贮过程、删除视图、读表数据、更新整个表数据等等,非常详细。
摘录:《程序员秘书》--源代码--数据库--SQL数据库各种操作
详见:http;//www.psec.net.cn
摘录:《程序员秘书》--源代码--数据库--SQL数据库各种操作
详见:http;//www.psec.net.cn
#4
string connectString = this.txtConn.Text.Trim();//数据库连接字符串,用一个字符窜来接受txt中的文本
using (SqlConnection con = new System.Data.SqlClient.SqlConnection(connectString))
{
con.Open();//打开连接
SqlCommand sqlCmd = new System.Data.SqlClient.SqlCommand();//创建一个Command对象
sqlCmd.Connection = con;
sqlCmd.CommandText = "UPDATE…… or DELETE……";//sql语句
sqlCmd.ExecuteNonQuery();
con.Close();//(联接结束)
}
using (SqlConnection con = new System.Data.SqlClient.SqlConnection(connectString))
{
con.Open();//打开连接
SqlCommand sqlCmd = new System.Data.SqlClient.SqlCommand();//创建一个Command对象
sqlCmd.Connection = con;
sqlCmd.CommandText = "UPDATE…… or DELETE……";//sql语句
sqlCmd.ExecuteNonQuery();
con.Close();//(联接结束)
}
#5
ADO.NET中的多数据表操作浅析
http://blog.csdn.net/zhzuo/archive/2004/08/06/67016.aspx
http://blog.csdn.net/zhzuo/archive/2004/08/06/67037.aspx
透过vs.net数据窗体向导看Ado.net
http://blog.csdn.net/zhzuo/archive/2005/01/03/238273.aspx
http://blog.csdn.net/zhzuo/archive/2004/08/06/67016.aspx
http://blog.csdn.net/zhzuo/archive/2004/08/06/67037.aspx
透过vs.net数据窗体向导看Ado.net
http://blog.csdn.net/zhzuo/archive/2005/01/03/238273.aspx