六种不同的connectionString的运用


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//string connectionString = "Data Source=(local);Integrated Security=SSPI;Initial Catalog=pubs";

//六种不同的connectionString的运用,均已通过编译,并且连接数据库成功
//string connectionString = "server=(local);database=pubs;uid=sa;pwd=";
//string connectionString ="Data Source=Localhost;Initial Catalog=pubs;Persist Security Info=True;User ID=sa;Password=";
//string connectionString = "server=127.0.0.1;database=pubs;uid=sa;pwd=";
string connectionString = "server=Localhost;database=pubs;uid=sa;pwd=";

     //另外Data Source如果是本地机的话,可以用IP127.0.0.1代替
//string connectionString = ConfigurationManager.ConnectionStrings["PubsConnectionString"].ConnectionString;
SqlConnection connection = new SqlConnection(connectionString);

//try
//{

// connection.Open();

// string commandString = "select * from authors";

// SqlCommand command = new SqlCommand(commandString, connection);
// SqlDataReader datareader = command.ExecuteReader(CommandBehavior.CloseConnection);

// GridView1.DataSource = datareader;
// GridView1.DataBind();
//}
//finally
//{
// connection.Close();
//}

string commandString = "select * from authors";
SqlCommand command=new SqlCommand (commandString,connection);
SqlDataAdapter dataadapter = new SqlDataAdapter(command);

DataSet dataset = new DataSet();
dataadapter.Fill(dataset,"authors");

GridView1.DataSource = dataset;
GridView1.DataBind();

}
}