2

I am trying to run a test query using sql. I know it is a simple concept, but i have tried everything I could find online and the following does not even run. It shows no errors but it does not run.

private static SqlConnection conn = new SqlConnection("<connection string>");

public static void connect()
{
    conn.Open();
    SqlCommand command = new SqlCommand("spTester 'this is tested'", conn);
    command.ExecuteScalar();
    conn.Close();
}
9
  • 1
    you are doing it wrong buddy Commented Mar 24, 2014 at 6:19
  • What is the expected result? Commented Mar 24, 2014 at 6:20
  • spTester is a stored procedure that inserts a column into a table named tester. I checked this table it seems to be empty. However when I run "spTester 'this is tested'" as it is in the db, it inserts a row Commented Mar 24, 2014 at 6:22
  • 1
    Why do you use ExecuteScalar() instead of ExecuteNoQuery()? Commented Mar 24, 2014 at 6:22
  • 1
    @Neville Nazerane: I'm very sorry for the typo: "ExecuteNonQuery()" Commented Mar 24, 2014 at 6:49

4 Answers 4

6

It seems that you want something like that:

private static void connect() {
  // static SqlConnection conn is a bad idea, local variable is much better
  // Do not forget to dispose IDisposable: using(...) {...}
  using (SqlConnection conn = new SqlConnection("<connection string>")) {
    // Do not forget to dispose IDisposable: using(...) {...}
    using (SqlCommand command = new SqlCommand("spTester", conn)) {
      // You're executing procedure, not ordinal SQL
      command.CommandType = CommandType.StoredProcedure;
      // It seems, that you should provide a parameter to your procedure:
      //TODO: Change "@ParameterName" to actual one
      command.Parameters.Add(new SqlParameter("@ParameterName", "this is tested"));

      // You don't need any result value be returned
      command.ExecuteNonQuery();
    }
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

tried and still no output. I guessed there is something wrong with my connection string although it doesnt throw an error.
StoredProcedure does not work since it has a variable as well and is run as an sql
If the "spTester" has a parameter (which is "this is tested") you should explictly declare it either with StoredProcedure or Text command type. See my edit. Do not forget to change @ParameterName to the real parameter name
1
public static void connect()
{
    conn.Open();
    SqlCommand command = new SqlCommand("spTester 'this is tested'", conn);
    command.CommandType = CommandType.StoredProcedure;
   SqlDataAdapter da = new SqlDataAdapter(cmd);
    conn.Close();
}

try doing this.. as u probably forgot to mention command.CommandType = CommandType.StoredProcedure; line

10 Comments

CommandType throws a but as not defined. Do I have to include something using 'using'?
Put tht inside try catch @NevilleNazerane
i just realized whatever connection string i use it throws no error. Is there a way to test the connection strin gin the first place?
Have u taken name space using System.Data.SqlClient;?
this is my declaration using System; using System.Data.SqlClient;
|
0

this is a simple example it will let you get started using SQLCOMMAND

using (SqlConnection conn = new SqlConnection(connString))
{
  SqlCommand cmd = new SqlCommand("SELECT * FROM whatever 
                                   WHERE id = 5", conn);
    try
    {
        conn.Open();
        newID = (int)cmd.ExecuteScalar();
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}

Comments

0
Try this :

public static void connect()
{
    conn.Open();
    SqlCommand command = new SqlCommand("spTester", conn);
    command.CommandType = CommandType.StoredProcedure;
    command.AddWithValue("@Parameter1","this is tested")
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    conn.Close();
}

Comments

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.