|
-
February 4th, 2009, 09:27 AM
#1
I can create a SQL connection, but can't run any queries
Hello,
I'm attempting to run a sql query from my console app, but my problem is that I can create a connection, but cannot run any queries, can you advise.
Here's my code to add a connection
Code:
using System;
using System.Data;
using System.Data.SqlClient;
namespace Connection_Sql
{
class Connection_Sql
{
static void Main()
{
SqlConnection thisConnection = new SqlConnection(
@"Server=********;" +
"initial catalog=***;" +
"uid=***;" +
"password=********;");
// create SqlCommand
SqlCommand thisCommand = new SqlCommand();
Console.WriteLine("Command created.");
try
{
// open connection
thisConnection.Open();
Console.WriteLine("Connection opened.");
}
catch (SqlException ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
thisConnection.Close();
Console.WriteLine("Connection Closed.");
}
}
}
}
This produces the code below, showing that I created the connection.
Code:
Command created.
Connection Opened
Connection Closed.
Press any key to continue . . .
But when I try to add a sql query, doing the following code:
Code:
ing System;
using System.Data;
using System.Data.SqlClient;
namespace Connection_Sql
{
class Connection_Sql
{
static void Main()
{
// create SqlConnection object
SqlConnection thisConnection = new SqlConnection(
@"Server=*******;" +
"initial catalog=***;" +
"uid=***;" +
"password=********;");
// create SqlCommand
SqlCommand thisCommand = new SqlCommand("SELECT order_num, return_num from returns where order_num='123456'", thisConnection);
Console.WriteLine("Command created.");
try
{
// open connection
thisConnection.Open();
SqlDataReader thisReader = null;
thisReader = thisCommand.ExecuteReader();
while (thisReader.Read())
{
Console.WriteLine(thisReader["Column1"].ToString());
Console.WriteLine(thisReader["Column2"].ToString());
}
}
catch (SqlException ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
thisConnection.Close();
Console.WriteLine("Connection Closed.");
}
}
}
}
I get the following error, regardless of the query I add. My queries work, so I'm not sure why I can create a connection, but not run any queries???
Code:
Command created.
System.Data.SqlClient.SqlException: Invalid object name '**********'.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolea
n breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception
, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObj
ect stateObj)
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cm
dHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, Tds
ParserStateObject stateObj)
at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()
at System.Data.SqlClient.SqlDataReader.get_MetaData()
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, Run
Behavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBe
havior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehav
ior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult
result)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehav
ior, RunBehavior runBehavior, Boolean returnStream, String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, S
tring method)
at System.Data.SqlClient.SqlCommand.ExecuteReader()
at Connection_Sql.Connection_Sql.Main() in C:\Documents and Settings\Visual Studio 2005\Projects\SQLCONNECTION\SQLCONNECTION\Program.cs:l
ine 30
Connection Closed.
Press any key to continue . . .
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|