0

I am create a database utility and I seem to not be able to get my connectionstring correct.

   SqlConnection conn = new SqlConnection();
   conn.ConnectionString =  
        "Data Source=.\\SQLExpress;" +
        "User Instance=true;" +
        "Integrated Security=true;" +
        "AttachDbFilename=|DataDirectory|ConfigurationData.mdf;";

I believe this is in the correct format. As for the data source, my sql server is SQLExpress which runs sql server 2008 R2. My database is named ConfigurationData. Am I missing something?

When I run it, it opens the database - I assume it does since it does not through exception - but when I try inserting into a table, it does not actually insert it yet it executes the command.

conn.Open();
        try
        {

          SqlCommand comm = new SqlCommand("INSERT INTO Test " + "(id,number) " + "  VALUES(" + 10 + " , " + 12 + ")", conn);
          comm.ExecuteNonQuery();
            Console.WriteLine("Database is created successfully", "MyProgram");
        }
        catch (Exception ex)
        {

        }
        finally
        {
            if ((conn.State == ConnectionState.Open))
            {
                conn.Close();
            }
        }
7
  • 1
    Most likely you're copying the .mdf file every time you build/debug, which overwrites the database the program is using with the copy you have in your solution. Can you verify this? Commented Jul 16, 2013 at 18:44
  • how do you check to see if it was inserted Commented Jul 16, 2013 at 18:44
  • I am new to databases and they way I check is that when I insert into the table, I place a breakpoint and open the table to check to see if it was inserted Commented Jul 16, 2013 at 18:46
  • Open SSMS and check in it if it is inserted or not Commented Jul 16, 2013 at 18:51
  • 1
    9 out of 10 times this is a case of acting and verifying on two different databases. I would suggest you open SSMS and hand type the same insert statement Commented Jul 16, 2013 at 19:19

5 Answers 5

1

EDIT Just remembered that I had answered a similar question a while back. Check it out:

Why can't I insert data into local database (SQL Compact Edition) with C#?

I don't think it is the connection string issue. But for your reference, a good site to refer to is http://www.connectionstrings.com/sql-server-2008/

You would need one of these:

Attach a database file, located in the data directory, on connect to a local SQL Server Express instance

Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf;Database=dbname;
Trusted_Connection=Yes;

Attach a database file on connect to a local SQL Server Express instance

Server=.\SQLExpress;AttachDbFilename=C:\MyFolder\MyDataFile.mdf;Database=dbname;
Trusted_Connection=Yes;

Using an User Instance on a local SQL Server Express instance The User Instance functionality creates a new SQL Server instance on the fly during connect. This works only on a local SQL Server instance and only when connecting using windows authentication over local named pipes. The purpose is to be able to create a full rights SQL

Server instance to a user with limited administrative rights on the computer.

Data Source=.\SQLExpress;Integrated Security=true;
AttachDbFilename=C:\MyFolder\MyDataFile.mdf;User Instance=true;

To use the User Instance functionality you need to enable it on the SQL Server. This is done by executing the following command: sp_configure 'user instances enabled', '1'. To disable the functionality execute sp_configure 'user instances enabled', '0'.

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

2 Comments

It throws and error saying that the .\ unrecognized escaped sequence. would it be find if it was .\\?
Don't use "\\". Format the string like @"C:\...\"
0

try (local) instead of dot, dot is not recognized in Win XP

conn.ConnectionString =  
        "Data Source=(local)\\SQLExpress;" +
        "User Instance=true;" +
        "Integrated Security=true;" +
        "AttachDbFilename=|DataDirectory|ConfigurationData.mdf;";

Comments

0

You're SQL Statement isn't right also, and you should use parameters, but here is what you should have

SqlCommand comm = new SqlCommand("INSERT INTO Test (id, number)  VALUES('" + 10 + " ', '" + 12 + "')", conn);

Comments

0

Why not just use the SqlConnectionStringBuilder class?:

SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = @"(local)\SQLExpress";
builder.UserInstance = true;
builder.IntegratedSecurity = true;
builder.AttachDBFilename = "|DataDirectory|ConfigurationData.mdf";
SqlConnection conn = new SqlConnection(builder.ConnectionString());

The output:

    "Data Source=(local)\\SQLExpress;AttachDbFilename=|DataDirectory|ConfigurationData.mdf;Integrated Security=True;User Instance=True"

Comments

0

One of the way to do this is to add your connection string in web.config file as shown below:

Jus click on the properties of database on the database explorer. There you will find connectionstring in its properties. Jus add it in connectionstring below.

<configuration>
    <connectionStrings>
        <add name="ConnectionName" connectionString="your connection string" providerName="System.Data.SqlClient"/>
    </connectionStrings>
</configuration>

Then in the page you can store it in string or directly refer to your connection as shown below:

Connection con=new SqlConnection();
con.ConnectionString=ConfigurationManager.ConnectionStrings["connString"].ToString();

and I suspect your insert statement is not properly declared.

Just try this:

SqlCommand comm = new SqlCommand("INSERT INTO Test (id,number) VALUES('10' ,'12')", con);

That's all from my part... Hope it helped you..

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.