2023年6月21日发(作者:)

C#--SqlServer--sql语句拼接和带参数的SQL语句以下是学习笔记:⼀,常⽤的sql语句写法1,直接写⼊的对于⾮值类型,两边各加⼀个单引号(')【例1】 int Id =1; string Name="lui"; dText="insert into TUserLogin values("+Id+",'"+Name+"')"; 因为Id是数值,所以在传递的时候只需要在sql字符串中⽤"+Id+"即可实现,⽽ Name是字符串,所以在传递的时候还需要在"+Name+"两边各加⼀个单引号(')来实现;2,占位符的字符串拼接【例2】 //定义SQL语句 string sql = "insert into Students (StudentName,Gender,DateOfBirth,StudentIdNo,Age,PhoneNumber,StudentAddress,ClassId)"; sql += "values( '{0}', '{1}', '{2}','{3}', '{4}','{5}','{6}','{7}')"; sql = (sql, "包⼦", "男", "1989-07-11", "2345678", 20, "", "杭州", 1); //调⽤通⽤数据访问类 int result = eNonQuery(sql,null); ine(result);  以上,在实际开发中,直接写字符串或使⽤占位符的⽅式,会有潜在的危险。(注⼊式攻击)以下解决问题:⼆,通过带参数的SQL语句和存储过程实现(推荐)【例1】转载:/ymtianyu/article/details/8818192//定义数据库连接string strconn = "Data Source=xxx;user id=sa;pwd=;initial catalog=gltest";SqlConnection Conn = new SqlConnection(strconn);//开启数据库();//定义操作语句,其中@即为所引⽤的参数string sql = "insert into users(name,pwd) values (@name,@pwd)";//定义参数及相关属性和要传⼊的值SqlCommand cmd = new SqlCommand(sql, Conn);(new SqlParameter("@name", ar, 50));(new SqlParameter("@pwd", ar, 50));ters["@name"].Value = ;ters["@pwd"].Value = ;//执⾏sql语句eNonQuery();//关闭数据库(); ()添加参数到参数集第⼀个参数是要添加的参数名第⼆个参数是参数的数据类型第三个是数据长度Parameters的作⽤就是把存储过程执⾏结束后得到的参数传到程序⾥  【例2】 定义SQL语句 string sql = "insert into Products (ProductId,ProductName,UnitPrice,Unit,Discount,categoryId)"; sql += "values(@ProductId,@ProductName,@UnitPrice,@Unit,@Discount,@categoryId)"; //封装参数 //SqlParameter param1 = new SqlParameter("@ProductId", "1000900002"); //SqlParameter param2 = new SqlParameter("@ProductName", "汽⽔"); ////。。。。 //SqlParameter[] param = new SqlParameter[] { param1, param2 }; //以上太⿇烦,我们可以直接定义参数数组 SqlParameter[] param = new SqlParameter[] { new SqlParameter("@ProductId", "1000900007"), new SqlParameter("@ProductName", "汽⽔"), new SqlParameter("@UnitPrice", 5), new SqlParameter("@Unit", "箱"), //new SqlParameter("@Discount",0),//参数为0报错 // new SqlParameter("@Discount","0"), //特别注意这个地⽅没有加双引号的,会出现参数找不到的问题。 //new SqlParameter("@Discount","1"),// 加双引号的"1"是可以执⾏的 new SqlParameter("@Discount",(object)0), // 当这个地⽅是0的时候,要使⽤objet转化成引⽤类型(官⽅解释) new SqlParameter("@categoryId", 3), }; //调⽤通⽤数据访问类 int result = eNonQuery(sql, param); ine(result);  Helper类: public static int ExecuteNonQuery(string sql, SqlParameter[] param = null) { //创建链接对象 SqlConnection conn = new SqlConnection(connString); //创建⼀个命令执⾏对象

SqlCommand cmd = new SqlCommand(sql, conn); //建议直接⽤构造⽅法 try { (); //添加参数 if (param != null) { ge(param); } return eNonQuery(); } catch (Exception ex) { //如果有必要可以在这个的记录⽇志.... //注意:我们通⽤的数据访问类是“底层”地⽅⽅法,我们捕获到异常,必须还得告诉调⽤者具体的异常。 string errorMsge = "调⽤ExecuteNonQuery⽅法发⽣异常,具体异常信息:" + e; // throw ex; //可以直接把ex对象跑出去,也可以做⼆次封装 throw new Exception(errorMsge); } finally //表⽰前⾯不管是否发⽣异常,都会执⾏的代码段 { (); } }  三,SqlParameter的两个⽅法1,Add⽅法SqlParameter sp = new SqlParameter("@name","Pudding");(sp);sp = new SqlParameter("@ID","1");(sp);  2,AddRange⽅法SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@name","Pudding"),new SqlParameter("@ID","1") };ge(paras);  

2023年6月21日发(作者:)

C#--SqlServer--sql语句拼接和带参数的SQL语句以下是学习笔记:⼀,常⽤的sql语句写法1,直接写⼊的对于⾮值类型,两边各加⼀个单引号(')【例1】 int Id =1; string Name="lui"; dText="insert into TUserLogin values("+Id+",'"+Name+"')"; 因为Id是数值,所以在传递的时候只需要在sql字符串中⽤"+Id+"即可实现,⽽ Name是字符串,所以在传递的时候还需要在"+Name+"两边各加⼀个单引号(')来实现;2,占位符的字符串拼接【例2】 //定义SQL语句 string sql = "insert into Students (StudentName,Gender,DateOfBirth,StudentIdNo,Age,PhoneNumber,StudentAddress,ClassId)"; sql += "values( '{0}', '{1}', '{2}','{3}', '{4}','{5}','{6}','{7}')"; sql = (sql, "包⼦", "男", "1989-07-11", "2345678", 20, "", "杭州", 1); //调⽤通⽤数据访问类 int result = eNonQuery(sql,null); ine(result);  以上,在实际开发中,直接写字符串或使⽤占位符的⽅式,会有潜在的危险。(注⼊式攻击)以下解决问题:⼆,通过带参数的SQL语句和存储过程实现(推荐)【例1】转载:/ymtianyu/article/details/8818192//定义数据库连接string strconn = "Data Source=xxx;user id=sa;pwd=;initial catalog=gltest";SqlConnection Conn = new SqlConnection(strconn);//开启数据库();//定义操作语句,其中@即为所引⽤的参数string sql = "insert into users(name,pwd) values (@name,@pwd)";//定义参数及相关属性和要传⼊的值SqlCommand cmd = new SqlCommand(sql, Conn);(new SqlParameter("@name", ar, 50));(new SqlParameter("@pwd", ar, 50));ters["@name"].Value = ;ters["@pwd"].Value = ;//执⾏sql语句eNonQuery();//关闭数据库(); ()添加参数到参数集第⼀个参数是要添加的参数名第⼆个参数是参数的数据类型第三个是数据长度Parameters的作⽤就是把存储过程执⾏结束后得到的参数传到程序⾥  【例2】 定义SQL语句 string sql = "insert into Products (ProductId,ProductName,UnitPrice,Unit,Discount,categoryId)"; sql += "values(@ProductId,@ProductName,@UnitPrice,@Unit,@Discount,@categoryId)"; //封装参数 //SqlParameter param1 = new SqlParameter("@ProductId", "1000900002"); //SqlParameter param2 = new SqlParameter("@ProductName", "汽⽔"); ////。。。。 //SqlParameter[] param = new SqlParameter[] { param1, param2 }; //以上太⿇烦,我们可以直接定义参数数组 SqlParameter[] param = new SqlParameter[] { new SqlParameter("@ProductId", "1000900007"), new SqlParameter("@ProductName", "汽⽔"), new SqlParameter("@UnitPrice", 5), new SqlParameter("@Unit", "箱"), //new SqlParameter("@Discount",0),//参数为0报错 // new SqlParameter("@Discount","0"), //特别注意这个地⽅没有加双引号的,会出现参数找不到的问题。 //new SqlParameter("@Discount","1"),// 加双引号的"1"是可以执⾏的 new SqlParameter("@Discount",(object)0), // 当这个地⽅是0的时候,要使⽤objet转化成引⽤类型(官⽅解释) new SqlParameter("@categoryId", 3), }; //调⽤通⽤数据访问类 int result = eNonQuery(sql, param); ine(result);  Helper类: public static int ExecuteNonQuery(string sql, SqlParameter[] param = null) { //创建链接对象 SqlConnection conn = new SqlConnection(connString); //创建⼀个命令执⾏对象

SqlCommand cmd = new SqlCommand(sql, conn); //建议直接⽤构造⽅法 try { (); //添加参数 if (param != null) { ge(param); } return eNonQuery(); } catch (Exception ex) { //如果有必要可以在这个的记录⽇志.... //注意:我们通⽤的数据访问类是“底层”地⽅⽅法,我们捕获到异常,必须还得告诉调⽤者具体的异常。 string errorMsge = "调⽤ExecuteNonQuery⽅法发⽣异常,具体异常信息:" + e; // throw ex; //可以直接把ex对象跑出去,也可以做⼆次封装 throw new Exception(errorMsge); } finally //表⽰前⾯不管是否发⽣异常,都会执⾏的代码段 { (); } }  三,SqlParameter的两个⽅法1,Add⽅法SqlParameter sp = new SqlParameter("@name","Pudding");(sp);sp = new SqlParameter("@ID","1");(sp);  2,AddRange⽅法SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@name","Pudding"),new SqlParameter("@ID","1") };ge(paras);