Basic SQL stuff problem

I am a beginner in SQL. I have a problem with him. First of all, I know that I installed SQL 2005 Server n and then created ConsoleApp and the rest of the code is under:

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnection myConnection = new SqlConnection("user id=userid;" +
                                       "Password=validpassword;" +
                                       "Trusted_Connection=yes;" +
                                       "Data Source=localhost;" +
                                       "connection timeout=300");



            try
            {
                myConnection.Open();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }


            SqlCommand myCommand = new SqlCommand("INSERT INTO table (Column1, Column2) " +
                                      "Values ('string', 1)", myConnection);

            myCommand.ExecuteNonQuery();



        }
    }
}

      

WHEN I RUN THIS ... I get an Unhandled Exception of type "System.Data.SqlClient.SqlException" happened in System.Data.dll using myCommand.ExecuteNonQuery ();

Listen to me where I am going wrong. I just want to do simple things. Insert something n, then read it. But I am getting error even when inserting.

0


source to share


6 answers


You are not using a database. Query the home directory on the database connection string that contains the Table table. Or, INSERT INTO dbName..table VALUES ...



Also check out ConnectionStrings.com . Explore it, use it, love it.

+6


source


Your connection is missing the Initial Catalog parameter. It should be "Initial Catalog = DatabaseName". Also if you are using Trusted Connection you do not need user id and password parameters.



+3


source


Run my exception handling move to contain ExecuteNonQuery and then edit your question with a full exception.

        static void Main(string[] args)
        {
            SqlConnection myConnection = new SqlConnection("user id=userid;" +
                                       "Password=validpassword;" +
                                       "Trusted_Connection=yes;" +
                                       "Data Source=localhost;" +
                                       "connection timeout=300");


            try
            {
                myConnection.Open();

                SqlCommand myCommand = new SqlCommand("INSERT INTO table (Column1, Column2) " +
                                      "Values ('string', 1)", myConnection);
                myCommand.ExecuteNonQuery();    
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }

      

+2


source


When you installed SQL Server 2005, did you put it in mixed mode? To do this, you will need to enter your username and password. I'm not sure what .NET will do with username / password and trusted connection = true in the connection string. Are you getting an open connection?

You will also need the name of the database in your connection string.

I can't add a commit to your post, so I'll add it here. You need to run SQL Server Management Studio to access your database and future tables. Then go to help and start reading books online on how to create tables.

+1


source


There is either a problem with the database connection - wrong username, password and / or server, or the table and columns do not exist. I would put a breakpoint on the catch output and look at the exception to find out more.

0


source


If you are masking an exception or debugging a console application so you can see the details of the exception, you should be able to find out the full details of the error by looking at the exception message or inner exception.

As Chad pointed out, it could be a simple naming issue where the table or columns referenced in the query does not exist in the database you are pointing to. Alternatively, there may be a problem with the type of data you are trying to interpret. The DB server message that falls back on the exception will let you know more.

0


source







All Articles