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
 Reading and writing to a table

Author  Topic 

asher
Starting Member

36 Posts

Posted - 2012-12-21 : 05:43:50
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.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-12-21 : 06:34:16
do you've the table Customer2 in attached db?

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

Go to Top of Page

asher
Starting Member

36 Posts

Posted - 2012-12-21 : 06:47:20
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

52326 Posts

Posted - 2012-12-21 : 06:54:23
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

36 Posts

Posted - 2012-12-21 : 07:03:16
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

36 Posts

Posted - 2012-12-21 : 07:11:24
PS I snipped a couple of usings.
Go to Top of Page
   

- Advertisement -