首先把Model层和UI层的数据源一定要配置完成,按步骤会特别顺手。
1.我们来到Web.config中找到UI层配置的数据源的name:
<add name="ProDBConnectionString" connectionString="Data Source=.;Initial Catalog=ProDB;Integrated Security=True" providerName="System.Data.SqlClient" />
这个 name="ProDBConnectionString"。
2.我们来看DAL层的SqlHelper:
public class SqlHelper { //固定连接字符串 public static string strcon = ConfigurationManager.ConnectionStrings["ProDBConnectionString"].ConnectionString; /// <summary> /// 查询 /// </summary> /// <param name="sql"></param> /// <returns></returns> public static DataTable Cx(string sql) { DataTable dt=new DataTable(); SqlDataAdapter arAdapter=new SqlDataAdapter(sql,strcon); arAdapter.Fill(dt); return dt; } /// <summary> /// 增删改 /// </summary> /// <param name="sql"></param> /// <returns></returns> public static int Zsg(string sql) { int num = 0; SqlConnection sqlcon=new SqlConnection(strcon); SqlCommand cmd=new SqlCommand(sql,sqlcon); try { sqlcon.Open(); num = cmd.ExecuteNonQuery(); } finally { if (sqlcon.State==ConnectionState.Open) { sqlcon.Close(); } } return num; } }
我们的命名写的很通俗易懂,大家理解的也会很快~