存储过程的调用和Parameters数组的调用
记录一些杂记吧
1 declare @d datetime 2 set @d=GETDATE() 3 4 select * from [ACC_B2B].[dbo].ORDER_Head --where 1=1 5 6 select datediff(ms,@d,getdate()) --2453 --2406 7 8 9 给查询条件加上where 1=1 速度会快一点;
更多详细的看看这个sql语句中where 1=1和1=0的作用
里面的1=0快速建表:create table newtable as select * from oldtable where 1=0; 创建一个新表,而新表的结构与查询的表的结构是一样的。
1 引用地址:http://www.398588.com/Article/T5/46.aspx 2 返回一个存储过程的返回值。 3 方法代码: 4 public int SqlParm(string cmdText,SqlParameter[] Prams) 5 { 6 SqlConnection Conn = new SqlConnection(ConfigurationManager.ConnectionStrings[0].ConnectionString); 7 Conn.Open(); 8 SqlCommand Cmd = new SqlCommand(cmdText, Conn); 9 Cmd.CommandType = CommandType.StoredProcedure; 10 foreach (SqlParameter P in Prams) 11 { 12 Cmd.Parameters.Add(P); 13 } 14 Cmd.Parameters.Add(new SqlParameter("Return", SqlDbType.Int, 4, ParameterDirection.ReturnValue, false, 0, 0, string.Empty, DataRowVersion.Default, null)); 15 Cmd.ExecuteNonQuery(); 16 int RowNum = int.Parse(Cmd.Parameters["Return"].Value.ToString()); 17 return RowNum; 18 } 19 调用方法代码: 20 SqlParameter[] Paramrs={new SqlParameter("@Q",SqlDbType.VarChar,50)}; 21 Paramrs[0].Value = "abc"; //给@Q值。 22 Response.Write(SqlParm("过程名称",Paramrs); 23 以上仅供参考,本站原创文章,如有转载请带链接注明出自 网络自由人 24 25 1 SqlParameter[] paras= 26 2 { 27 3 new SqlParameter("@name","这里应该放@name的类型") 28 4 }; 29 5 SqlParameter parm = new SqlParameter("select_Con", "73468969715"); 30 6 DataTable myset = ExecuteStoredProcedure("select_Con", parm);
现在,我们要改写这样的语句,使用SqlParameter来做 SqlCommand SqlCmd = new SqlCommand(sql, SqlConn); SqlParameter _userid = new SqlParameter("uid", SqlDbType.Int); _userid.Value = Request.QueryString["u_id"]; SqlCmd.Parameters.Add(_userid);
1 public static DataTable ExecuteStoredProcedure(string ProcNme, params SqlParameter[] paramemters) 2 { 3 SqlDataReader reader = null; 4 DataTable dt = new DataTable(); 5 using (SqlConnection conn=new SqlConnection(ConfigurationManager.ConnectionStrings["Connection B2B"].ToString())) 6 { 7 conn.Open(); 8 using (SqlCommand cmd=conn.CreateCommand()) 9 { 10 SqlTransaction st = conn.BeginTransaction(); 11 cmd.Transaction = st; 12 try 13 { 14 cmd.CommandText = ProcNme;// 存储过程与Command牵连在一起了 15 cmd.CommandType = CommandType.StoredProcedure;//command执行的类型是存储过程,不再是sql语句 16 17 cmd.Parameters.AddRange(paramemters); 18 reader = cmd.ExecuteReader();//返回发的是DataReader 下面的方法将reader转成datatable 19 dt= ConvertDataReaderToDataTable(reader); 20 return dt; 21 22 //cmd.Parameters.AddRange(paras); 23 //SqlDataAdapter da = new SqlDataAdapter(); 24 //da.SelectCommand = cmd; 25 //DataSet ds = new DataSet(); 26 //da.Fill(ds, "StuInfo"); 27 //dataGridView1.DataSource = ds.Tables["StuInfo"]; 28 } 29 catch (Exception) 30 { 31 32 throw; 33 } 34 } 35 } 36 } 37 38 public static DataTable ConvertDataReaderToDataTable(SqlDataReader reader) 39 { 40 try 41 { 42 DataTable objDataTable = new DataTable(); 43 int intFieldCount = reader.FieldCount; 44 for (int intCounter = 0; intCounter < intFieldCount; ++intCounter) 45 { 46 objDataTable.Columns.Add(reader.GetName(intCounter), reader.GetFieldType(intCounter)); 47 } 48 49 objDataTable.BeginLoadData(); 50 51 object[] objValues = new object[intFieldCount]; 52 while (reader.Read()) 53 { 54 reader.GetValues(objValues); 55 objDataTable.LoadDataRow(objValues, true); 56 } 57 reader.Close(); 58 objDataTable.EndLoadData(); 59 60 return objDataTable; 61 62 } 63 catch (Exception ex) 64 { 65 throw new Exception("转换出错!", ex); 66 } 67 68 }
1 public override bool ValidateUser(string username, string password) 2 { 3 SqlConnection sqlconn = new SqlConnection("Data Source=12345;Initial Catalog=TestDB01;Integrated Security=True"); 4 SqlCommand sqlcmd = new SqlCommand("select UserID, UserName from [UserInfo] where UserName = @userName and UserAge = @userAge", sqlconn); 6 try 7 { 8 sqlconn.Open(); 9 10 sqlcmd.Parameters.Add(new SqlParameter("@userName", SqlDbType.NVarChar, 50)); 11 sqlcmd.Parameters["@userName"].Value = username.Trim(); 12 sqlcmd.Parameters.Add(new SqlParameter("@userAge", SqlDbType.SmallInt, 2)); 13 sqlcmd.Parameters["@userAge"].Value = password; 14 15 SqlDataReader sqlRd = sqlcmd.ExecuteReader(); 16 if (sqlRd.HasRows) 17 { 18 return true; 19 } 20 21 return false; 22 } 23 catch (Exception ex) 24 { 25 throw new Exception(ex.Message); 26 } 27 }
public static DataTable SpReturnTable(string storedProcName, SqlParameter[] parameters, string connstring) { try { using (SqlConnection conStr = new SqlConnection(connstring)) { DataTable dt = new DataTable(); conStr.Open(); SqlTransaction trans = null; //创建事务 trans = conStr.BeginTransaction(); try { SqlCommand comStr = new SqlCommand(storedProcName, conStr); comStr.Transaction = trans; comStr.CommandType = CommandType.StoredProcedure; //设置执行类型为存储过程 foreach (SqlParameter parameter in parameters) { comStr.Parameters.Add(parameter); } using (SqlDataAdapter sda = new SqlDataAdapter()) { sda.SelectCommand = comStr; sda.Fill(dt); } trans.Commit(); //提交事务 return dt; } catch (Exception ex) { string a = ex.Message; trans.Rollback(); //回滚事务 return null; } finally { conStr.Close(); } } } catch (Exception ex) { return null; } }
人各有命,上天注定,有人天生为王,有人落草为寇。脚下的路,如果不是你自己的选择,那么旅程的终点在哪,也没人知道。你会走到哪,会遇到谁,都不一定。