G
Guest
Hi All,
when running this program
using System;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Threading;
namespace TestDataAdapter
{
class Class1
{
private static OleDbConnection _connection;
[STAThread]
static void Main(string[] args)
{
_connection = createConnection();
Thread dataThread = new Thread(new ThreadStart(dataTester));
dataThread.Start();
Console.ReadLine();
}
private static OleDbConnection createConnection()
{
string connectionString = "Provider=SQLOLEDB.1;Integrated
Security=SSPI;Persist Security Info=False;Initial
Catalog=Northwind;Data Source=127.0.0.1;Use Procedure for Prepare=1;Auto
Translate=True;Packet Size=4096;Use
Encryption for Data=False;Tag with column collation when possible=False";
OleDbConnection conn = null;
try
{
conn = new OleDbConnection(connectionString);
conn.Open();
Console.WriteLine("Connection opened successfully");
}
catch(Exception)
{
Console.WriteLine("Connection could not be opened");
}
return conn;
}
private static void dataTester()
{
Thread.Sleep(1000);
DataSet ds = new DataSet();
//_connection = createConnection();
string sql = "select LastName from Employees";
OleDbCommand cmd = new OleDbCommand(sql, _connection);
OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
adapter.Fill(ds);
cmd = null;
Console.WriteLine("DataSet filled");
}
}
}
with a SQL Server the dataThread does not finish the call to apdater.Fill
until the main thread is finished. If I let the dataThread create the
connection or if I use SqlConnection instead this is not a problem.
An alternative approach is to let the main thread start a new thread doing
exactly the same as the main thread does in this example. This also solves
the problem. Can anyone explain this to me? Is this a bug?
when running this program
using System;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Threading;
namespace TestDataAdapter
{
class Class1
{
private static OleDbConnection _connection;
[STAThread]
static void Main(string[] args)
{
_connection = createConnection();
Thread dataThread = new Thread(new ThreadStart(dataTester));
dataThread.Start();
Console.ReadLine();
}
private static OleDbConnection createConnection()
{
string connectionString = "Provider=SQLOLEDB.1;Integrated
Security=SSPI;Persist Security Info=False;Initial
Catalog=Northwind;Data Source=127.0.0.1;Use Procedure for Prepare=1;Auto
Translate=True;Packet Size=4096;Use
Encryption for Data=False;Tag with column collation when possible=False";
OleDbConnection conn = null;
try
{
conn = new OleDbConnection(connectionString);
conn.Open();
Console.WriteLine("Connection opened successfully");
}
catch(Exception)
{
Console.WriteLine("Connection could not be opened");
}
return conn;
}
private static void dataTester()
{
Thread.Sleep(1000);
DataSet ds = new DataSet();
//_connection = createConnection();
string sql = "select LastName from Employees";
OleDbCommand cmd = new OleDbCommand(sql, _connection);
OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
adapter.Fill(ds);
cmd = null;
Console.WriteLine("DataSet filled");
}
}
}
with a SQL Server the dataThread does not finish the call to apdater.Fill
until the main thread is finished. If I let the dataThread create the
connection or if I use SqlConnection instead this is not a problem.
An alternative approach is to let the main thread start a new thread doing
exactly the same as the main thread does in this example. This also solves
the problem. Can anyone explain this to me? Is this a bug?