none
Connect to SQL Server RRS feed

  • Question

  • HELLO

     

    I want to know a basic question about how to use c# to conect to SQL Server.

     

    Have any web page to teach us to connect SQL Server using OLEDB ?

    Tuesday, August 14, 2007 3:21 AM

Answers

  • Erik,

    you can get an example of a generic connection in the MSDN documentation of the OleDbConnection class.

     

    HTH

    --mc

    Tuesday, August 14, 2007 3:56 AM
  •  

    In brief without trawling through examples you need to include the System.Data.SqlClient; namespace.

     

    You need a SQLConnection Object, this contains the connection string and an open and close method amongst other things. The DB connection should be opened by you and closed by you and closed when there's an exception.

     

    You also need a SQLCommand object, this contains the Query or procedure and parameters you want to run, when you create a SQLCommand object you pass the SQL Connection into the constructor.

     

    The SQLCommand contains methods for running yuor query/proc and getting back results.

     

    You will still need to look it up to get an overview but at least you can understand the principle.

     

    Here's a small code snippet that may or may not be 100% as i'm making it up Smile

     

    SqlConnection sqlConn = new SqlConnection("DataSource=Yay;Initial Catalog=Nay;Integrated Security=Indeed!;")

     

    SqlCommand sqlComm = new SqlCommand(sqlConn);

     

    sqlComm.CommandType = CommandType.StoredProcedure;

    sqlComm.CommandText = "proc_WE";

    sqlComm.Parameters.Add("@Param", "paramValue");

     

    sqlConn.Open();

     

    try

    {

    sqlComm.ExecuteNonQuery();

    }

    catch....

     

    That will execute a stored proc and return nothing Big Smile. In the proc you can use Raiserror to raise a SQLException with a message of your choosing. There is much more to it than that.

    Tuesday, August 14, 2007 9:03 AM

All replies

  • Erik,

    you can get an example of a generic connection in the MSDN documentation of the OleDbConnection class.

     

    HTH

    --mc

    Tuesday, August 14, 2007 3:56 AM
  •  

    In brief without trawling through examples you need to include the System.Data.SqlClient; namespace.

     

    You need a SQLConnection Object, this contains the connection string and an open and close method amongst other things. The DB connection should be opened by you and closed by you and closed when there's an exception.

     

    You also need a SQLCommand object, this contains the Query or procedure and parameters you want to run, when you create a SQLCommand object you pass the SQL Connection into the constructor.

     

    The SQLCommand contains methods for running yuor query/proc and getting back results.

     

    You will still need to look it up to get an overview but at least you can understand the principle.

     

    Here's a small code snippet that may or may not be 100% as i'm making it up Smile

     

    SqlConnection sqlConn = new SqlConnection("DataSource=Yay;Initial Catalog=Nay;Integrated Security=Indeed!;")

     

    SqlCommand sqlComm = new SqlCommand(sqlConn);

     

    sqlComm.CommandType = CommandType.StoredProcedure;

    sqlComm.CommandText = "proc_WE";

    sqlComm.Parameters.Add("@Param", "paramValue");

     

    sqlConn.Open();

     

    try

    {

    sqlComm.ExecuteNonQuery();

    }

    catch....

     

    That will execute a stored proc and return nothing Big Smile. In the proc you can use Raiserror to raise a SQLException with a message of your choosing. There is much more to it than that.

    Tuesday, August 14, 2007 9:03 AM
  • In addition, you can find some ConnectionStrings to SQL Server 2005 using OLEDB here.

    http://www.connectionstrings.com/?carrier=sqlserver2005

     

    Check www.connectionstrings.com for ConnectionStrings to various database servers and data files.

     

    I hope that can help you.

    Thursday, August 16, 2007 3:23 AM