绑定
string sql="select * from table1"
SqlHelper.ExecuteDataset(连接字符串Conn, CommandType.Text, sql )
新增修改删除数据
string sql = "update m_user set u_sex=@sex,u_name=@name,u_pwd=@pwd,u_address=@address,u_email=@email,u_phone=@phone,u_Birthday=@Birthday where u_name=@username";
OleDbParameter[] paras =
{
new OleDbParameter("@sex", OleDbType.VarChar, 10) ,
new OleDbParameter("@name", OleDbType.VarChar, 20) ,
new OleDbParameter("@pwd", OleDbType.VarChar, 20) ,
new OleDbParameter("@address",OleDbType.VarChar, 50),
new OleDbParameter("@email",OleDbType.VarChar, 20),
new OleDbParameter("@phone",OleDbType.VarChar, 20),
new OleDbParameter("@Birthday",OleDbType.VarChar, 20),
new OleDbParameter("@username",OleDbType.VarChar, 20)
};
paras[0].Value = DropSex.SelectedValue;
paras[1].Value = txtName.Text;
paras[2].Value = txtPwd.Text;
paras[3].Value =txtAddress.Text;
paras[4].Value =txtEmail.Text;
paras[5].Value =txtPhone.Text;
paras[6].Value = txtBirthday.Text;
paras[7].Value = Session["username"].ToString();
新增修改删除数据 (带参数化) SqlHelper.ExecuteNonQuery(连接字符串Conn, CommandType.Text, sql, paras)
paras是参数。
string sql="insert into table(name) values ('"+ txtname.Text+"')"
//string sql="delete from table where id= "+ txtid.Text+" "
//string sql="update table2 set name='"+ txtname.Text+"' where id= "+ txtid.Text+" "
新增修改删除数据 (拼接SQL 无参数) SqlHelper.ExecuteNonQuery(连接字符串Conn, CommandType.Text, sql)
如果是运行存储过程就把CommandType.Text改为CommandType.StoredProcedure
CommandType.Text 表示运行的是本页string的SQL
如果是用事物执行一系列操作的就这样
using (OleDbConnection conn = new OleDbConnection(myConnectString))
{
using (OleDbTransaction st = myConnection.BeginTransaction())
{
try{
SqlHelper.ExecuteNonQuery(st, CommandType.Text, sql, paras);//执行添加数据1
SqlHelper.ExecuteNonQuery(st, CommandType.Text, sql2, paras);//执行添加数据2
st.Commit();
}
catch (OleDbException ee)
{
st.Rollback();
cmdreturn.Text = ee.Message.ToString();
cmdreturn.Text = "Rollback";
//throw ee;
}
}
}
如果是SQL就把OleDbException中的OleDb OleDbConnection 中的OleDb 替换换成Sql
using using System.Data.OleDb; 改为 using System.Data.Sql; using System.Data.SqlClient