.NET平台访问远程SQL Server 2000数据库的方法 在.NET平台中要访问远程SQL Server 2000数据库,首先要设置好数据库连接字段:string connectionString="data source=数据库服务器名;initial catalog=数据库名;uid=sa;pwd=;",如果sa用户有密码要在“pwd=”后面加上密码,“uid”、“pwd”这两个参数也可以设为其他用户名和其对应的密码。其次,还要把操作系统中的Guest用户设置为启用状态。
protected void Button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection sql_connection = new SqlConnection (@"server=169.254.64.16;database=TrainingInfoManag;uid=sa;pwd=;");
sql_connection.Open();
SqlCommand cmd = sql_connection.CreateCommand();
cmd.CommandText = @"SELECT ClassName from ClassInfo where ClassID='1'";
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
TextBox1.Text = reader["ClassName"].ToString();
reader.Close();
sql_connection.Close();
}catch(System.Data.SqlClient.SqlException excep)
{ MessageBox.Show("Error!/n" + "/n Exception Description:" + excep.Message + "/n/n欲获得更多信息,请与网站管理员联系!", "DataBase Exception"); }
}