-2

I know I have done this a few times before but for the life of me can't remember how. I have a database I have created and I want to make a software that only inputs information into the database. The program works but my sql connection is the problem. So to test it out I basically tried to do it direct inserting hard-coded info but it still will not go. where am I going wrong?:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.Common;
    using System.Data.SqlClient;
    using System.Data.Sql;

    namespace InventoryTracker
    {
        public partial class Form1 : Form
        {
          public Form1()
            {
                InitializeComponent();
            }


            public static void CreateCommand()
            {
                SqlConnection myConnection = new SqlConnection("User Id=Jab" + "password=''" + "Data Source=localhost;" + "Trusted_Connection=yes;" + "database=InventoryTracker;" + "Table=Inventory;");

                try
                {
                    myConnection.Open();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
                SqlCommand myCommand = new SqlCommand("INSERT INTO Inventory (ItemName, SerialNumber, Model, Department, Quantity, Notes) " + "Values (string,string,string,string, 1, string)", myConnection);

            }
    }
}

Thank you in advance! :-)

6
  • What error are you getting? Commented Aug 30, 2013 at 19:06
  • 1
    Where do you execute the query? Commented Aug 30, 2013 at 19:06
  • You don't need to use + in your connection string. And, nobody can know right connection string except you. Commented Aug 30, 2013 at 19:07
  • Here is a sample to go msdn.microsoft.com/en-us/library/… Commented Aug 30, 2013 at 19:09
  • I don't think you need username and password in the connection string if you say it's a trusted connection... trusted connection is telling SQL to use windows authentication. Username and password tells it to use SQL authentication. Commented Aug 30, 2013 at 19:13

5 Answers 5

3

Your sql connection string is messed up, you need semi-colons between all parameters and your parameters are messed up too. I.e., something like

"Server=localhost;Database=InventoryTracker;Trusted_Connection=True;"

You are mixing trusted mode and specifying the user id -- trusted connection means to use your windows login credentials.

TableName does not go in the connection string.

This site is great for connection string examples http://www.connectionstrings.com/sql-server-2008

You SQL command, "INSERT INTO Inventory (ItemName ..." is pretty messed up too. Should be something like

INSERT INTO Inventory (ItemName ...) values(@ItemName ...)

You then pass in the values like

myCommand.Parameters.Add("ItemName", SqlType.VarChar).Value = "Dozen Eggs";

See Insert data into SQL Server from C# code for a simple example

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

1 Comment

Thank you all. All replies helped
3

Just use the SqlConnectionStringBuilder class.

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnectionstringbuilder.aspx

Instead of:

"User Id=Jab" + "password=''" + "Data Source=localhost;" + "Trusted_Connection=yes;" + "database=InventoryTracker;" + "Table=Inventory;");

Try:

SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.UserID = "Jab";
builder.Password = "";
builder.DataSource = "localhost";
builder.InitialCatalog = "InventoryTracker";

1 Comment

I've never used SqlConnectionStringBuilder. My connection strings have always been in a configuration file so that it is not compiled into the app. But a potentially useful alternative I've never notice before. +1
3
// Don't put table name in your connection string
    string connection_str = "Data Source = localhost ; uid = db_user; pwd = db_pass; database = db_name; ";
     conn = new SqlConnection(connection_str);
     conn.Open();

Comments

1

Try changing

"User Id=Jab" + "password=''" + "Data Source=localhost;" + "Trusted_Connection=yes;" + "database=InventoryTracker;" + "Table=Inventory;"

to

"User Id=Jab; " + "Password=''; " + "Data Source=localhost; " + "Trusted_Connection=yes; " + "Initial Catalog=InventoryTracker;"

(Changed upper/lower case, "Database" to "Initial Catalog", removed "Table" and added ";")


Also, you might want to try replacing "Data Source" by "Server".

1 Comment

Data Source and Server are synonyms. Database and Initial Catalog are synonyms. Connection string is not case sensitive though password, etc. in the string are case sensitive.
0

Don't forget to call myCommand.ExecuteNonQuery to get it to actually execute your query. Without this, you are just creating a command, but not running it.

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.