1
public static void Sql_Operation(string userID)
2
{
3
using (SqlConnection myConncetion = new SqlConnection(ConfigurationManager.AppSettings["ConnString"]))
4
using (SqlCommand myCommand = new SqlCommand("select UserID, UserName, UserPwd from WHERE userID = @userID ", myConncetion))
5
{
6
try
7
{
8
SqlParameter myParameter = new SqlParameter("@userID",SqlDbType.Int,4);
9
myParameter.Value = Int32.Parse(userID);
10
myCommand.Parameters.Add(myParameter);
11
myConncetion.Open();
12
myCommand.ExecuteNonQuery();
13
}
14
catch (Exception err)
15
{
16
throw new Exception("Err info:"+err.Message.ToString())
17
}
18
finally
19
{
20
myDataAdapter.Dispose();
21
myConncetion.Close();
22
myConncetion.Dispose();
23
}
24
}
25
}
public static void Sql_Operation(string userID)2

{ 3
using (SqlConnection myConncetion = new SqlConnection(ConfigurationManager.AppSettings["ConnString"]))4
using (SqlCommand myCommand = new SqlCommand("select UserID, UserName, UserPwd from WHERE userID = @userID ", myConncetion))5

{6
try7

{8
SqlParameter myParameter = new SqlParameter("@userID",SqlDbType.Int,4);9
myParameter.Value = Int32.Parse(userID);10
myCommand.Parameters.Add(myParameter);11
myConncetion.Open();12
myCommand.ExecuteNonQuery();13
}14
catch (Exception err)15

{16
throw new Exception("Err info:"+err.Message.ToString())17
}18
finally19

{20
myDataAdapter.Dispose();21
myConncetion.Close();22
myConncetion.Dispose();23
}24
}25
}
本文提供了一个使用C#进行SQL操作的示例代码,演示了如何通过参数化查询从数据库中检索用户信息。此代码展示了连接数据库、执行参数化查询及异常处理的方法。
937

被折叠的 条评论
为什么被折叠?



