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
 Reading and writing to a table
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

asher
Starting Member

Netherlands
36 Posts

Posted - 12/21/2012 :  05:43:50  Show Profile  Reply with Quote
I created a database using this code:
string connection_string = @"Data Source=.\SQLExpress;Integrated Security=true;AttachDbFilename=C:\NewDatabase\NewTest2.mdf;User Instance=true;"; (having been gratefully advised that this is the right way).
The database creation was successful in as much that the the database appears in the directory.
I used the same connection string in the code to create a table and to read and write to it.
Running the code, I get the same (reading and writing) error, namely " invalid object name 'Customer2' "
Can anyone suggest a reason for this?

Asher.

Edited by - asher on 12/21/2012 07:34:03

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 12/21/2012 :  06:34:16  Show Profile  Reply with Quote
do you've the table Customer2 in attached db?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

asher
Starting Member

Netherlands
36 Posts

Posted - 12/21/2012 :  06:47:20  Show Profile  Reply with Quote
quote:
Originally posted by visakh16

do you've the table Customer2 in attached db?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





Yes the table was created in the database being written to and read from.
Table creation, writing and reading is being attempted with the connection string as posted previously.
Reading is being attempted like so:
cmnd.CommandText = "SELECT name, company,street,pobox,city,telephone,email,customer_key FROM Customers2 WHERE customer_key LIKE search_argument";

Asher.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 12/21/2012 :  06:54:23  Show Profile  Reply with Quote
the only reason i would reckon getting that error is either table doesnt exist or is not created on your default schema

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

asher
Starting Member

Netherlands
36 Posts

Posted - 12/21/2012 :  07:03:16  Show Profile  Reply with Quote
quote:
Originally posted by visakh16

the only reason i would reckon getting that error is either table doesnt exist or is not created on your default schema

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





The table was created like so:

(Any good?)

using System.IO;

namespace FilesAndDatabase
{
public sealed class CustomerCreateTable
{
public CustomerCreateTable()
{
try
{
if (Directory.Exists(@"c:\NewDatabase\"))
{
new Warning("Now creating a Customers table.");
string connection_string = @"Data Source=.\SQLExpress;Integrated Security=true;AttachDbFilename=C:\NewDatabase\NewTest2.mdf;User Instance=true;";
SqlConnection connection = new SqlConnection(connection_string);
connection.Open();
SqlCommand command = new SqlCommand();
command.CommandText =
"CREATE TABLE Customers2"
+ "(name VARCHAR,"
+ "company VARCHAR,"
+ "street VARCHAR,"
+ "pobox VARCHAR,"
+ "city VARCHAR,"
+ "telephone VARCHAR,"
+ "email VARCHAR,"
+ "customer_key VARCHAR NOT NULL,"
+ "PRIMARY KEY(customer_key))";
command.Connection = connection;
command.ExecuteNonQuery();
connection.Close();
new TableAvailabilityWrite();
}
}
catch (Exception x)
{
new Warning("Table creation: " + x.Message);
}
}
}
}



Go to Top of Page

asher
Starting Member

Netherlands
36 Posts

Posted - 12/21/2012 :  07:11:24  Show Profile  Reply with Quote
PS I snipped a couple of usings.
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.06 seconds. Powered By: Snitz Forums 2000