I'm having issue with running a query in c# that updates a table in sql server.
In total I'm updating about 2500 records in my table. However there are about 350 rows where the acq_rank field is not being updated. So I have checked the query by looking at the cmdText variable and the parameter values. If I copy these values and run the query in sql server manually (bottom of this post) the query updates the table. I'm not sure what is happening.
UPDATE
Comparing the records that did update and those that didn't it appears any companies with a comma in their name are the ones not being updated. How do I get round this?
When debugging the code I can see the company names are normally "some name" however when they contain a comma however it looks like "\"some name with comma\""
C#
for (int i = 0; i < rankList.Count; i++)
{
string cmdText;
if(rankList[i].company_id == "NULL")
cmdText = "update MA_DEALS_COUNTRY set acq_rank = @rank where deal_id = @deal and company_id is null and co_name = @coname";
else
cmdText = "update MA_DEALS_COUNTRY set acq_rank = @rank where deal_id = @deal and company_id = @co_id and co_name = @coname";
_command = new SqlCommand(cmdText, _connection);
if (rankList[i].company_id == "NULL")
{
_command.Parameters.Add("@deal", System.Data.SqlDbType.VarChar).Value = rankList[i].DealId;
_command.Parameters.Add("@coname", System.Data.SqlDbType.VarChar).Value = rankList[i].CoName;
_command.Parameters.Add("@rank", System.Data.SqlDbType.Int).Value = rankList[i].Rank;
}
else
{
_command.Parameters.Add("@deal", System.Data.SqlDbType.VarChar).Value = rankList[i].DealId;
_command.Parameters.Add("@co_id", System.Data.SqlDbType.VarChar).Value = rankList[i].company_id;
_command.Parameters.Add("@coname", System.Data.SqlDbType.VarChar).Value = rankList[i].CoName;
_command.Parameters.Add("@rank", System.Data.SqlDbType.Int).Value = rankList[i].Rank;
}
_command.ExecuteNonQuery();
Console.WriteLine("row " + i.ToString());
}
SQL Server Manual Update
I have included both update statement - one for when the company_id is null and one when it is not
declare @rank int
declare @deal varchar(20)
declare @co_id varchar(5)
declare @coname varchar(max)
set @rank = 0
set @deal = '136344MM'
set @co_id = 'AB240'
set @coname = 'Company Capital Partners, Inc.'
update MA_DEALS_COUNTRY set acq_rank = @rank where deal_id = @deal and company_id is null and acquirer_name = @coname
update MA_DEALS_COUNTRY set acq_rank = @rank where deal_id = @deal and company_id = @co_id and acquirer_name = @coname