G
Gene Wirchenko
Dear C-Sharpies:
I have written a very simple program that communicates with SQL
Server Express 2008. It executes the following commands:
string[] SQLCmdList=
{
"use master" // 0
,
"drop database SQLServerAccess" // 1
,
"create database SQLServerAccess" // 2
,
"use SQLServerAccess" // 3
,
"drop table ClientBasic" // 4
,
"create table ClientBasic"+ // 5
" ("+
" CliCode char(3) not null primary key,"+
" CliName varchar(40) not null,"+
" DisbMin money not null"+
" )"
,
"insert into ClientBasic"+ // 6
" (CliCode,CliName,DisbMin)"+
"values"+
" ('ABC','A Better Company',100.00),"+
" ('BCD','Binary Code Designers',50.00),"+
" ('CDE','Computer Data Entry',200.00),"+
" ('DEF','Data Entry Functions',50.00),"+
" ('EFG','Entry for Gene',25.00)"
,
"insert into ClientBasic"+ // 7
" (CliCode,CliName,DisbMin)"+
"values"+
" ('FGH','Flowers'' Gift Honey',100.00)"
,
"select * from ClientBasic" // 8
};
and it works.
I have tried to add transactions to it (around each insert), and
I get a very puzzling error message. Here is my code:
try
{
if (i==6 || i==7) // insert
{
SqlTransaction
oTransaction=Conn.BeginTransaction();
int RowsReturned=SQLCmd1.ExecuteNonQuery();
Console.WriteLine("Rows Returned: {0}",
RowsReturned);
oTransaction.Commit();
}
else // not insert
{
int RowsReturned=SQLCmd1.ExecuteNonQuery();
Console.WriteLine("Rows Returned: {0}",
RowsReturned);
}
}
catch (Exception e)
{
Console.WriteLine("Command {0}
exception:\n"+e.Message, i);
}
and here is the relevant output:
Executing command 6
insert into ClientBasic (CliCode,CliName,DisbMin)values ('ABC','A
Better Company
',100.00), ('BCD','Binary Code Designers',50.00), ('CDE','Computer
Data Entry',2
00.00), ('DEF','Data Entry Functions',50.00), ('EFG','Entry for
Gene',25.00)
Command 6 exception:
*****ExecuteNonQuery requires the command to have a transaction when
the connection a
ssigned to the command is in a pending local transaction. The
Transaction prope
rty of the command has not been initialized.*****
Executing command 7
insert into ClientBasic (CliCode,CliName,DisbMin)values
('FGH','Flowers'' Gift H
oney',100.00)
Command 7 exception:
SqlConnection does not support parallel transactions.
Without the begin transaction and commit method calls, my code
works fine. What, please, is wrong with my transaction code? What
does the asterisk-flagged error message mean?
Sincerely,
Gene Wirchenko
I have written a very simple program that communicates with SQL
Server Express 2008. It executes the following commands:
string[] SQLCmdList=
{
"use master" // 0
,
"drop database SQLServerAccess" // 1
,
"create database SQLServerAccess" // 2
,
"use SQLServerAccess" // 3
,
"drop table ClientBasic" // 4
,
"create table ClientBasic"+ // 5
" ("+
" CliCode char(3) not null primary key,"+
" CliName varchar(40) not null,"+
" DisbMin money not null"+
" )"
,
"insert into ClientBasic"+ // 6
" (CliCode,CliName,DisbMin)"+
"values"+
" ('ABC','A Better Company',100.00),"+
" ('BCD','Binary Code Designers',50.00),"+
" ('CDE','Computer Data Entry',200.00),"+
" ('DEF','Data Entry Functions',50.00),"+
" ('EFG','Entry for Gene',25.00)"
,
"insert into ClientBasic"+ // 7
" (CliCode,CliName,DisbMin)"+
"values"+
" ('FGH','Flowers'' Gift Honey',100.00)"
,
"select * from ClientBasic" // 8
};
and it works.
I have tried to add transactions to it (around each insert), and
I get a very puzzling error message. Here is my code:
try
{
if (i==6 || i==7) // insert
{
SqlTransaction
oTransaction=Conn.BeginTransaction();
int RowsReturned=SQLCmd1.ExecuteNonQuery();
Console.WriteLine("Rows Returned: {0}",
RowsReturned);
oTransaction.Commit();
}
else // not insert
{
int RowsReturned=SQLCmd1.ExecuteNonQuery();
Console.WriteLine("Rows Returned: {0}",
RowsReturned);
}
}
catch (Exception e)
{
Console.WriteLine("Command {0}
exception:\n"+e.Message, i);
}
and here is the relevant output:
Executing command 6
insert into ClientBasic (CliCode,CliName,DisbMin)values ('ABC','A
Better Company
',100.00), ('BCD','Binary Code Designers',50.00), ('CDE','Computer
Data Entry',2
00.00), ('DEF','Data Entry Functions',50.00), ('EFG','Entry for
Gene',25.00)
Command 6 exception:
*****ExecuteNonQuery requires the command to have a transaction when
the connection a
ssigned to the command is in a pending local transaction. The
Transaction prope
rty of the command has not been initialized.*****
Executing command 7
insert into ClientBasic (CliCode,CliName,DisbMin)values
('FGH','Flowers'' Gift H
oney',100.00)
Command 7 exception:
SqlConnection does not support parallel transactions.
Without the begin transaction and commit method calls, my code
works fine. What, please, is wrong with my transaction code? What
does the asterisk-flagged error message mean?
Sincerely,
Gene Wirchenko