短い:
C#コードのSQLステートメントが機能していません。with(nolock)
コードを壊しています。
詳細:
以下は私のエラーとエラーが発生するコードです。コードはSQLServerデータベースに接続し(接続コードは正常に機能します)、クエリを実行することになっています。このクエリは、URIが「blah」であるすべてのイベントのIPアドレスを取得します。問題は、with(nolock)
私が使用する必要がある私のコマンドのようです。すべてのT-SQLクエリのグループ標準であるため、これを使用する必要があります。
私はしばらくグーグルで検索しましたが、私の問題に合うものは何もないようで、見つけた修正はまだ機能していません。私のコードやリンクについての助けをいただければ幸いです。
エラー:
System.Data.SqlClient.SqlExceptionがキャッチされましたMessage=キーワード'with'の近くの構文が正しくありません。このステートメントが共通テーブル式、xmlnamespaces節、または変更追跡コンテキスト節である場合、前のステートメントはセミコロンで終了する必要があります。
Source =.NetSqlClientデータプロバイダーErrorCode=-2146232060 Class = 15 LineNumber = 1 Number = 319 Procedure = "" Server = State = 1
コード:
try
{
//create sql reader to display data
SqlDataReader myReader = null;
//create string to enter data into database
string insString = @"select c_ip from @dates with(nolock) where cs_uri like 'blah'";
SqlCommand myCommand = new SqlCommand(insString, DbConnection);
//populate and sanitize parameters
myCommand.Parameters.Add("@dates", SqlDbType.VarChar, 100);
myCommand.Parameters["@dates"].Value = currentdate;
//execute the command
myReader = myCommand.ExecuteReader();
//read all results and print them to output
while (myReader.Read())
{
//get IPs
String ipmix = myReader["c_ip"].ToString();
mainIPs.Add(ipmix);
}
}
catch (Exception e)
{
Console.WriteLine("The query connection to the datebase has timed out.\n");
Console.WriteLine(e.ToString());
Console.ReadLine();
}
解決:
コードを次の場所から変更します。
//create string to enter data into database
string insString = @"select c_ip from @dates with(nolock) where cs_uri like 'blah'";
に:
//create string to enter data into database
string insString = @"select c_ip from " + currentdate + " with(nolock) where cs_uri like '%blah'";