Asp.net connection mssql 2005 code and instance

Source: Internet
Author: User
Tags mssql mysql tutorial connectionstrings

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"/>

%>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.