我知道在一般的程序中可以这样实现
SqlConnection conn=new SqlConnection
SqlCommand comm=new SqlCommand("update tb1 set vName=@vName,iAge=@iAge where ID=@id",conn);
SqlParameter parm1=new SqlParameter("@vName",SqlDbType.NVarChar,50);
parm1.Value=((TextBox)e.Item.FindControl("name")).Text;
comm.Parameters.Add(parm1);
conn.Open();
comm.ExecuteNonQuery();
conn.Close(); 但我现在是三层结构,
一个操作类 user.cs
public int addUser(string userName,string password,string email)
{
string sql="insert into T_User(UserName,UserPwd,Email,RegDate,LastDate)values('"+userName+"','"+password+"','"+email+"','"+System.DateTime.Now+"','"+System.DateTime.Now+"')";
return ExecuteSql(sql);
}
一个数据库操作类 db.cs
public int ExecuteSql(string sql)
{
SqlCommand cmd=new SqlCommand(sql,con);
try
{
con.Open();
cmd.ExecuteNonQuery();
return 1;
}
catch
{
return 0;
}
finally
{
cmd.Dispose();
con.Close();
}
}
但问题是我不知道怎么给cmd 添加SqlParameter了,要怎么实现呢,或者有什么其它更好的防SQL注入的方法
SqlConnection conn=new SqlConnection
SqlCommand comm=new SqlCommand("update tb1 set vName=@vName,iAge=@iAge where ID=@id",conn);
SqlParameter parm1=new SqlParameter("@vName",SqlDbType.NVarChar,50);
parm1.Value=((TextBox)e.Item.FindControl("name")).Text;
comm.Parameters.Add(parm1);
conn.Open();
comm.ExecuteNonQuery();
conn.Close(); 但我现在是三层结构,
一个操作类 user.cs
public int addUser(string userName,string password,string email)
{
string sql="insert into T_User(UserName,UserPwd,Email,RegDate,LastDate)values('"+userName+"','"+password+"','"+email+"','"+System.DateTime.Now+"','"+System.DateTime.Now+"')";
return ExecuteSql(sql);
}
一个数据库操作类 db.cs
public int ExecuteSql(string sql)
{
SqlCommand cmd=new SqlCommand(sql,con);
try
{
con.Open();
cmd.ExecuteNonQuery();
return 1;
}
catch
{
return 0;
}
finally
{
cmd.Dispose();
con.Close();
}
}
但问题是我不知道怎么给cmd 添加SqlParameter了,要怎么实现呢,或者有什么其它更好的防SQL注入的方法
public bool ModifyUser(
string sUserID,
string sUserName,
string sUserMSN,
string sUserEmail,
string sUserProject,
string sUserJobTitle)
{ SqlDatabase db = this.GetDb();
DbCommand cmd = db.GetStoredProcCommand("ModifyUser");
db.AddInParameter(cmd, "UserID", DbType.String, sUserID);
db.AddInParameter(cmd, "UserName", DbType.String, sUserName);
db.AddInParameter(cmd, "UserMSN", DbType.String, sUserMSN);
db.AddInParameter(cmd, "UserEmail", DbType.String, sUserEmail);
db.AddInParameter(cmd, "UserProject", DbType.String, sUserProject);
db.AddInParameter(cmd, "UserJobTitle", DbType.String, sUserJobTitle);
return db.ExecuteNonQuery(cmd) == 1 ? true : false;
}
简单吧
你已经用参数提供到SQL语句里了
用参数的方法不会传入危险字符的
http://www.soeye.cn/article/2006-3/136.html
1.db.cs中写的函数是
updateDbInfo(string tablename,string[] TableColsName,string[] ColsValues,string[] ColsTypes,int[] ColsFieldLenth)
{
for(循环tablecolsname,组装SQL语句)
{
...
}
...
//代码省略若干...
...
//做判断,取出类型
for(....)
{
//这里用Switch或If来判断
if(ColsTypes[i].ToString()=="int")
{
}
else if(...)
{}
else if(...)
...
else
{
//默认类型,没有匹配的都在这里
}
}
}我想参数就不用我解释了吧?!直接应该可以看懂的
你试试
虽然有点麻烦,但是功能可以很好的被实现(至少我现在是这么觉得的),如果有更好的方法请告知!
好运!
当然,麻烦之处在于要在user.cs组装好字符串数组,而后一并送入
2.如果你是想解决SQL注入的问题,请你在接收userName等参数的时候replace掉一写敏感的字符就行,如select,update,insert等。
3.最好的解决方法是用存储过程。
这是个基类 public virtual void Add(string[] p_strFields,object[] p_objValues)
{
if(p_strFields == null)
{
MessageBox.Show("列不能为空!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
} if(p_strFields != null && p_strFields.Length != p_objValues.Length)
{
MessageBox.Show("列和值不对应!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
} string strSQL;
strSQL = string.Format("insert into {0}(",this.TableName);
for(int i= 0; i < p_strFields.Length; i++)
{
strSQL = strSQL + string.Format("{0}",p_strFields[i]);
strSQL = strSQL + (i == p_strFields.Length - 1 ? ")":",");
}
strSQL = strSQL + " values(";
for(int i = 0; i < p_objValues.Length; i++)
{
strSQL = strSQL + string.Format("{0}",p_objValues[i]);
strSQL = strSQL + (i == p_objValues.Length - 1 ? ")" : ",");
}
SqlConnection sqlCn = new SqlConnection(this.strConn);
try
{
sqlCn.Open();
}
catch(System.Exception ex)
{
MessageBox.Show("打开数据库出错:"+ex.Message,"提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
SqlTransaction sqlTran = sqlCn.BeginTransaction();
SqlCommand sqlCmd = sqlCn.CreateCommand();
sqlCmd.CommandText = strSQL;
sqlCmd.Transaction = sqlTran;
try
{
sqlCmd.ExecuteNonQuery();
sqlTran.Commit();
}
catch(System.Exception ex)
{
MessageBox.Show("执行SQL命令出错:"+ex.Message,"提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
sqlTran.Rollback();
return;
}
finally
{
sqlCmd.Dispose();
sqlTran.Dispose();
sqlCn.Close();
}
return;
}重写的类
public void Add()
{
string[] a_strFields = new string[7];
a_strFields[0] = "operatorID";
a_strFields[1] = "operator";
a_strFields[2] = "password";
a_strFields[3] = "hospitalID";
a_strFields[4] = "officeID";
a_strFields[5] = "purview";
a_strFields[6] = "creator";
object[] a_objValues = new object[7];
a_objValues[0] = "'" + this.ID + "'";
a_objValues[1] = "'" + this.Name + "'";
a_objValues[2] = "'" + this.Pwd + "'";
a_objValues[3] = "'" + this.HospitalID + "'";
a_objValues[4] = "'" + this.OfficeID + "'";
a_objValues[5] = "'" + this.Purview + "'";
a_objValues[6] = "'" + this.Creator + "'"; base.Add (a_strFields, a_objValues,true);
}还没找到其他比较好的方法,有的话告诉我一下
QQ:32296899
难道在你那个类中,sql就不能写成传参数的形式么?
我还以为是什么三层....
你的方法,我看懂了,也想过,但我觉得好麻烦啊,
我想问一下sqlparameter可以建数组不?如果可以,这样的话就简单多了
为:public int ExecuteSql(string sql,SqlParameter arrayPara)
参数:arrayPara是SqlParameter型数组。在 ExecuteSql方法 内遍历arrayPara数组,用Sqlcommand.Parameters.Add()即可。