日期:2014-05-17 浏览次数:20826 次
public static string conn = "data source=PINGGUOSHUAI;initial catalog=yinhang;persist security info=true;user id=sa;password=123456"; //静态构造一个数据库打开的连接,ConfigurationSettings.AppSettings[0]为web.config配置文件中声明的数据库连接字符串这样的连接数据库语句,还用在web.config中再写一遍吗?两个文件里面在哪个里面写比较好点?
public static int ExecuteCommand(string safeSql)
{
SqlConnection sqlConnection = new SqlConnection(conn);
int result;
try
{
sqlConnection.Open();
SqlCommand sqlCommand = new SqlCommand(safeSql, sqlConnection);
result = sqlCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.Write(ex.Message);
throw ex;
}
finally
{
sqlConnection.Close();
}
return result;
}