0

Can anybody give an example for executing a T-SQL statement using C#?

1
  • 2
    The OP could have posted an attempt at an answer though. This is very, very basic stuff. Commented Aug 11, 2009 at 20:44

3 Answers 3

10

Do you mean something like this:

private static void ReadOrderData(string connectionString)
{
      string commandText = "SELECT OrderID, CustomerID FROM dbo.Orders;";
      using (SqlConnection connection = new SqlConnection(connectionString))
      {
            using (SqlCommand command = new SqlCommand(commandText, connection))
            {
                  connection.Open();
                  using (SqlDataReader reader = command.ExecuteReader())
                  {
                        while (reader.Read())
                        {
                              Console.WriteLine(String.Format("{0}, {1}", 
                                reader[0], reader[1]));
                        }
                  }
            }
      }
}

Or, perhaps something like:

static public int AddProductCategory(string newName, string connString)
{
    Int32 newProdID = 0;
    string sql =
        "INSERT INTO Production.ProductCategory (Name) VALUES (@Name); "
        + "SELECT CAST(scope_identity() AS int)";
    using (SqlConnection conn = new SqlConnection(connString))
    {
        SqlCommand cmd = new SqlCommand(sql, conn);
        cmd.Parameters.Add("@Name", SqlDbType.VarChar);
        cmd.Parameters["@Name"].Value = newName;
        try
        {
            conn.Open();
            newProdID = (Int32)cmd.ExecuteScalar();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
    return (int)newProdID;
}

Source: MSDN

Sign up to request clarification or add additional context in comments.

Comments

6

I suggest that you start with an ADO.NET turorial like this one

http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson01.aspx

How to use SQLCommand

http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson03.aspx

1 Comment

both Links have gone offline, unfortunately
0

Using a reader:

SqlConnection MSSQLConn = new SqlConnection("your connection string");
MSSQLConn.Open();
SqlCommand MSSQLSelectConsignment = new SqlCommand();
MSSQLSelectConsignment.CommandText = "select * from yourtable where blah = @blah";
MSSQLSelectConsignment.Parameters.AddWithValue("@blah", somestring);
MSSQLSelectConsignment.Connection = MSSQLConnOLD;
SqlDataReader reader = MSSQLSelectConsignment.ExecuteReader();

while (reader.Read())
{
...
}

To bring back a single value:

MSSQLSelectConsignment.CommandText = "select fieldname from yourtable where blah = @blah";
string yourstring = MSSQLSelectConsignment.ExecuteScalar().ToString();

or to bring back number of rows affected for updates etc:

MSSQLSelectConsignment.CommandText = "update yourtable set yourfield = 0 where blah = @blah";
int yourint = MSSQLSelectConsignment.ExecuteNonQuery();

Hope helps :)

2 Comments

Bad example. You're leaking resources.
it was just a quick write up, I'm always willing to learn how to improve my code. I didn't add any close/dispose statements I realise which I normally do

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.