Asp tutorial. net connecting mssql 2005 code and instances
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 = "connection successful! ";
}
Catch (SqlException S)
{
MessageBox. Show (S. Message );
}
Finally
{
SQL. Close ();
SQL. Dispose ();
}
// Asp.net tutorial connecting mssql 2005 method 2
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 username = '" + this. textBox_user.Text + "'", connection );
SqlDataReader mysql tutorial reader = selectCmd. ExecuteReader ();
If (mysqlreader. Read ())
{
If (this. textBox_password.Text = mysqlreader ["password"]. 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 ("no user ");
}
}
}
Catch
{
}
Connection. Close ();
}
// Other methods for connecting mssql 2005 to. net
Use the aspnet_regsql.exe command to create a database;
2. Connection string:
<Configuration>
<ConnectionStrings>
<Remove name = "LocalSqlServer"/>
<Add name = "LocalSqlServer"
ConnectionString = "Data Source = localhost;
Initial Catalog = apps tutorial ervicesdb;
Integrated Security = True"
ProviderName = "System. Data. SqlClient"/>
</ConnectionStrings>
</Configuration>
Note that the expression for adding the connection string here is Integrated with Windows Authentication (using Windows Integrated Security).
<Add name = "LocalSqlServer"
ConnectionString = "Data Source = localhost;
Initial Catalog = appservicesdb;
Integrated Security = True"
ProviderName = "System. Data. SqlClient"/>
You can use the following expression to authenticate SQL Server:
<Add name = "Sales"
ProviderName = "System. Data. SqlClient"
ConnectionString = "server = myserver; database = Products; uid = salesUser; pwd = sellMoreProducts"/>
%>