Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Database name

Author  Topic 

asher
Starting Member

36 Posts

Posted - 2012-12-16 : 15:06:36
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
Master Smack Fu Yak Hacker

5155 Posts

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

asher
Starting Member

36 Posts

Posted - 2012-12-16 : 17:33:20
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

36 Posts

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

asher
Starting Member

36 Posts

Posted - 2012-12-16 : 18:26:09
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
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-12-17 : 07:56:04
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

36 Posts

Posted - 2012-12-18 : 19:19:46
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

36 Posts

Posted - 2012-12-19 : 02:21:28
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

36 Posts

Posted - 2012-12-19 : 02:27:57
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
   

- Advertisement -