C# SqlParameter 实例

添加单个参数:

string conStr = ConfigurationManager.ConnectionStrings["DBConn"].ToString();
SqlConnection conn = new SqlConnection(conStr);
conn.Open();
string cmdText = "SELECT * FROM TEST WHERE Para=@Para";
SqlCommand cmd = new SqlCommand(cmdText, conn);
//添加单个参数
SqlParameter sp = new SqlParameter("@Para", "para001");
cmd.Parameters.Add(sp);
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
	Console.WriteLine(sdr[0].ToString()+ sdr[1].ToString());
}
Console.ReadKey();

添加多个参数:

string conStr = ConfigurationManager.ConnectionStrings["DBConn"].ToString();
SqlConnection conn = new SqlConnection(conStr);
conn.Open();
string cmdText = "SELECT * FROM TEST WHERE Para01=@Para01 and Para02=@Para02";
SqlCommand cmd = new SqlCommand(cmdText, conn);
//添加多个参数
SqlParameter[] sp = { 
	new SqlParameter("@Para01", "Para001") ,
	new SqlParameter("@Para02", "Para002")
};
cmd.Parameters.AddRange(sp);
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
	Console.WriteLine(sdr[0].ToString()+ sdr[1].ToString());
}
Console.ReadKey();

 

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页