Web config配置连接字符串

<connectionStrings>

  <add name="DBName" connectionString="data source='服务器名';User ID='用户名';Password='密码' ;Initial Catalog='数据库名'">

</connectionStrings>
  public static DataSet(string Sql)

    {

      using(SqlConnection conn=new SqlConnection(ConfigurationManager.ConnectionStrings["DBName"].ConnectionString))

        {

          conn.Open();

          using(SqlCommand cmd=conn.CreateCommand())

            {

              cmd.CommandText=Sql;

              SqlDataAdapter adapter=new SqlDataAdapter();

                                   adapter.SelectCommand = cmd;

              DataSet ds=new DataSet();

              adapter.Fill(ds);

              return ds;

            }

        }

    }

直接添加字符串连接

public static DataSet(string Sql)

    {

      using(SqlConnection conn=new SqlConnection(@"data source="服务器名";User ID="用户名";Password="密码";Initial Catalog="数据库名""))

        {

          conn.Open();

          using(SqlCommand cmd=conn.CreateCommand())

            {

              cmd.CommandText=Sql;

              SqlDataAdapter adapter=new SqlDataAdapter();

                                   adapter.SelectCommand = cmd;

              DataSet ds=new DataSet();

              adapter.Fill(ds);

              return ds;

            }

        }

    }

posted on 2011-04-18 12:16  高兴happy  阅读(495)  评论(0编辑  收藏  举报