locked
how to connect/add/retrieve data to MS Sql DB RRS feed

  • Question

  • User2059853125 posted

    I want to connect pages in asp.net website to remote mssql DB. Than next step is to add the data from user login dataform to DB table.

    and than on another page retrieving the data of the logged in user from the DB.

    How to connect to the remote DB using drivers in MSSql?

    Thursday, July 5, 2012 9:26 AM

Answers

  • User-1618234021 posted

    U need to use the System.Data.SqlClient namespace for that. Most important step is to create a connection with sql server. For that you need to create an object of SqlConenction class like:

    SqlConnection con = new SqlConnection("server=IP or url of remote server;database=name of database, user id=username;password=password of database");
    con.Open();

    Replace the connection string of the connection with actual ip, username, database name and password. After that you need to use SqlCommand and SqlDataReader for reading data. You can also use DataSet and SqlDataAdapter. See the following URL for more info:

    http://msdn.microsoft.com/en-us/library/dw70f090.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 5, 2012 9:42 AM
  • User-1509636757 posted

    There is no difference in between local machine DB or remote DB when you try to connect thru code. Important thing is correct connectionstring.

    Here are some solved threads as well as tutorials on how you can perform connect to MSSQL and add/retrieve data:

    How To: Connect to SQL using C# and SELECT INSERT UPDATE ? - ASP.NET Forums

    CodeProject: Simple ADO.NET Database Read, Insert, Update and Delete using C#.. Free source code and programming articles

    hope it helps./.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 5, 2012 9:43 AM
  • User-1501801757 posted

    ODBC SQL connections really isn't ideal anymore in .NET.  With ADO.NET and EntityFramework the connection string avenue is the way to take.  If you wrap the connection inside a using statement the closing of the connection will be self managed and not cause issues such as locks etc that you can get using the old ODBC style.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 5, 2012 2:30 PM

All replies

  • User-1618234021 posted

    U need to use the System.Data.SqlClient namespace for that. Most important step is to create a connection with sql server. For that you need to create an object of SqlConenction class like:

    SqlConnection con = new SqlConnection("server=IP or url of remote server;database=name of database, user id=username;password=password of database");
    con.Open();

    Replace the connection string of the connection with actual ip, username, database name and password. After that you need to use SqlCommand and SqlDataReader for reading data. You can also use DataSet and SqlDataAdapter. See the following URL for more info:

    http://msdn.microsoft.com/en-us/library/dw70f090.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 5, 2012 9:42 AM
  • User-1509636757 posted

    There is no difference in between local machine DB or remote DB when you try to connect thru code. Important thing is correct connectionstring.

    Here are some solved threads as well as tutorials on how you can perform connect to MSSQL and add/retrieve data:

    How To: Connect to SQL using C# and SELECT INSERT UPDATE ? - ASP.NET Forums

    CodeProject: Simple ADO.NET Database Read, Insert, Update and Delete using C#.. Free source code and programming articles

    hope it helps./.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 5, 2012 9:43 AM
  • User2059853125 posted

    I have done it using ConnectionString.

    Now I want to use connection driver, to establish the connection once and than to use the same connection for all the pages.

    Which connection driver should I use else than ODBC?

    Thursday, July 5, 2012 10:04 AM
  • User-1618234021 posted

    Well, ideally in a multi user environment, you should not leave the connection open for longer times. You should open the connection, do the db stuff and close it. You can do it on all pages.

    And since you are connecting to SQL server, you should use System.Data.SqlClient classes.

    Thursday, July 5, 2012 10:06 AM
  • User-1501801757 posted

    ODBC SQL connections really isn't ideal anymore in .NET.  With ADO.NET and EntityFramework the connection string avenue is the way to take.  If you wrap the connection inside a using statement the closing of the connection will be self managed and not cause issues such as locks etc that you can get using the old ODBC style.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 5, 2012 2:30 PM