SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Database name
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

asher
Starting Member

Netherlands
36 Posts

Posted - 12/16/2012 :  15:06:36  Show Profile  Reply with Quote
Re the following string:
string connection_string = @"Server=.//Sqlexpress;DataBase=NewTest;Integrated Security=SSPI";

The database file NewTest.mdf is located in the file NewDatabase.

Given the above, is the DataBase name in the connection_string correct?

Asher.

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 12/16/2012 :  16:32:47  Show Profile  Reply with Quote
The server name should be .\SqlExpress. Take a look at connectionstrings.com - might be very useful.
Go to Top of Page

asher
Starting Member

Netherlands
36 Posts

Posted - 12/16/2012 :  17:33:20  Show Profile  Reply with Quote
Yes that is the way I have it in the application.
But that is n o t the question I asked

Re the following string:
string connection_string = @"Server=.\SqlExpress;DataBase=NewTest;Integrated Security=SSPI";

The database file NewTest.mdf is located in the file NewDatabase.

Given the above, is the DataBase name in the connection_string correct?

Asher.
Go to Top of Page

asher
Starting Member

Netherlands
36 Posts

Posted - 12/16/2012 :  17:38:07  Show Profile  Reply with Quote
Sorry type should be string connection_string = @"Server=.\\SqlExpress;DataBase=NewTest;Integrated Security=SSPI";
Go to Top of Page

asher
Starting Member

Netherlands
36 Posts

Posted - 12/16/2012 :  18:26:09  Show Profile  Reply with Quote
quote:
Originally posted by sunitabeck

The server name should be .\SqlExpress. Take a look at connectionstrings.com - might be very useful.



I had a look. A lot of information, apparently no simple answers in the domain of SqlServer
Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 12/17/2012 :  07:56:04  Show Profile  Reply with Quote
Is it attached to the server? If you have SQL Server Management Studio, you can connect to (local)\SqlExpress and see if it is connected via the object explorer. If it is, you should use the database name that you see in object explorer with the following connection string. Although by default the database name and the mdf filename are same, it does not have to be, so use the name you see in SSMS instead of the red NewTest in the code below

 @"Data Source=.\SQLEXPRESS;Initial Catalog=NewTest;Integrated Security=SSPI";
If that does not work, presumably the database is not attached to the server. In that case, use the following connection string:

@"Data Source=.\SQLExpress;Integrated Security=true;AttachDbFilename=C:\Temp\NewTest.mdf;User Instance=true;";
Replace the path with the appropriate path where your mdf file is located.
Go to Top of Page

asher
Starting Member

Netherlands
36 Posts

Posted - 12/18/2012 :  19:19:46  Show Profile  Reply with Quote
Hi Sunita,

Thank you for your help.

I have used the second string to create a table. The code produced no errors (not any more that is :). I will need to write to the table and read from it to ascertain if all is well. Questions if I may: Your string doesn't start off with the word "server" -- why is that so? In the code for reading and writing, do I also use the term "Data Source"? What would the connection string be there? What I intended doing seems to be all wrong.

Asher.
Go to Top of Page

asher
Starting Member

Netherlands
36 Posts

Posted - 12/19/2012 :  02:21:28  Show Profile  Reply with Quote
SunitaBeck write

<snipped>
>@"Data Source=.\SQLExpress;Integrated Security=true;AttachDbFilename=C:\Temp\NewTest.mdf;User Instance=true;";
>Replace the path with the appropriate path where your mdf file is located.

After applying the above to the code to create a table, the table threw no exception when executed.
After applying the same to the code to write to the table, no exception was thrown by that code either, whereas the connection string I had previously caused an exception to be thrown.

Question: I would expect "AttachDbFilename=C:\Temp\NewTest.mdf" to connect the database instance permanently to the database. Requiring this code to write to the table seems to suggest that attaching occurs only temporarily while the code is being executed. Is this assumption correct?

Go to Top of Page

asher
Starting Member

Netherlands
36 Posts

Posted - 12/19/2012 :  02:27:57  Show Profile  Reply with Quote
Executing the code below is not throwing an exception when executed repeatedly. I would expect an exception to be thrown because the same key is being repeated. Why is no exception being thrown?

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

namespace FilesAndDatabase
{
public sealed class CustomerWrite
{
string name;
string company;
string street;
string pobox;
string city;
string telephone;
string email;
string customer_key;

//public CustomerWrite(string name, string company, string street, string pobox, string city, string telephone, string email, string customer_key)
public CustomerWrite()
{
this.name = "1";// name;
this.company ="2";// company;
this.street="3";//street;
this.pobox = "4";//pobox;
this.city ="5";//city;
this.telephone ="6";//telephone;
this.email="7";//email;
this.customer_key="8";//customer_key;

try
{
using(SqlConnection my_connection = new SqlConnection (@"Data Source=.\SQLExpress;Integrated Security=true;AttachDbFilename=C:\NewDatabase\NewTest.mdf;User Instance=true;"))
{
using (SqlDataAdapter my_adapter = new SqlDataAdapter("SELECT * From Customers", my_connection))
{
using (DataSet my_dataset = new DataSet())
{
using (SqlCommandBuilder bldr = new SqlCommandBuilder(my_adapter))
{
my_adapter.Fill(my_dataset, "Customers");
DataTable my_table = my_dataset.Tables[0];
DataRow dr = my_table.NewRow();
dr[0]= name;
dr[1]= company;
dr[2]= street;
dr[3]= pobox;
dr[4]= city;
dr[5]= telephone;
dr[6]= email;
dr[7]= customer_key;
my_adapter.Update(my_dataset, "Customers");
my_table.AcceptChanges();
}
}
}
}
}
catch (Exception x)
{
new Warning("Writing to Customers\r\n" + x.Message);
}
}
}
}
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.11 seconds. Powered By: Snitz Forums 2000