asp教程.net 连接mssql 2005代码与实例
SqlConnection sql = new SqlConnection(@"server=.sql2005;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=test;Data Source=7085360CB900427");
try
{
sql.Open();
if (sql.State == ConnectionState.Open)
label1.Text = "连接成功!";
}
catch (SqlException S)
{
MessageBox.Show(S.Message);}
finally
{
sql.Close();
sql.Dispose();
}
//asp.net教程连接mssql 2005方法二
private void button_logIn_Click(object sender, EventArgs e)
{
string connectionString = "Data Source=(local);Initial Catalog=system;"
+ "Integrated Security=SSPI;";
SqlConnection connection = new SqlConnection(connectionString);
try
{
connection.Open();
if (connection.State == ConnectionState.Open)
{
SqlCommand selectCmd = new SqlCommand("select * from User where 用户名 = ' " + this.textBox_user.Text + " ' ", connection);
SqlDataReader mysql教程reader = selectCmd.ExecuteReader();
if (mysqlreader.Read())
{
if (this.textBox_password.Text == mysqlreader["密码"].ToString())
{
uiLogin.UserName = this.textBox_user.Text;
uiLogin.Password = this.textBox_password.Text;this.DialogResult = DialogResult.OK;
}
else
{
nLoginCount++;
if (nLoginCount == MAX_LOGIN_COUNT)
{
this.DialogResult = DialogResult.Cancel;
}
else
{
MessageBox.Show("Invalid user name and password!");
this.textBox_user.Focus();
}
}}
else
{
throw new Exception("没有该用户 ");
}
}
}
catch
{
}
connection.Close();
}
//其它的mssql 2005与.net连接方法
使用aspnet_regsql.exe 命令创建数据库教程;
2,连接字符串:
connectionString="Data Source=localhost;
Initial Catalog=apps教程ervicesdb;
Integrated Security=True"
providerName="System.Data.SqlClient"/>
注意这里添加连接字符串的表达式,使用的是集成Windows身份验证(using Windows Integrated Security)的方式
connectionString="Data Source=localhost;
Initial Catalog=appservicesdb;
Integrated Security=True"
providerName="System.Data.SqlClient"/>
另外的一种方式,使用SQL Server的身份认证,可以使用下面的表达式:
providerName="System.Data.SqlClient"
connectionString= "server=myserver;database=Products;uid=salesUser;pwd=sellMoreProducts" />
%>