2016-06-13 8 views
-1

私は自分のコンピュータを再起動し、今では私にArgumentExceptionのエラーを与えているまで、このエラーで未処理だった:エラーとしてArgumentExceptionが未処理の私のコードの以下の部分は完全に罰金働いていた

An unhandled exception of type 'System.ArgumentException' occurred in System.Data.dll

Additional information: Invalid value for key 'integrated security'

{ 
    SqlConnection con = new SqlConnection(@"Data Source=M2192822\SHIFTREPORTS;Initial Catalog=ShiftReports;Integrated Security=ture"); 
    SqlDataAdapter sda = new SqlDataAdapter("Select Count(*) From [All of Production] where Tool ='" + MoldText.Text + "' and [Internal Part Number] ='" + IPNText.Text + "'", con); 
    DataTable dta = new DataTable(); 
    sda.Fill(dta); 
    MessageBox.Show("Part or Mold Number is Incorrect"); 
} 

if (dta.Rows[0][0].ToString() != "1") 
+3

がtrueではないことを示す接続文字列にアクセスできるようになります。 – Ian

答えて

3

メッセージには、tureIntegrated Securityの有効な値ではありません。 Integrated Security=trueを使用してください。

コードには他にも深刻な問題があります。まず、接続が閉じられていない。次に、文字列連結を使用してSQL文を作成しています。これは、SQLインジェクション攻撃に身をさらす確実な方法です。ユーザーが適切に接続を閉じると、パラメータ化クエリを使用して1';drop table Products;--

を入力した場合に何が起こるか想像して、実際に文字列の連結よりも簡単です:

var connString = @"Data Source=M2192822\SHIFTREPORTS;Initial Catalog=ShiftReports;Integrated Security=true"; 
var query="Select Count(*) From [All of Production] where Tool = @tool and [Internal Part Number] = @part"; 
DataTable dta = new DataTable(); 

using(var con = new new SqlConnection(connString)) 
using(var sda=new SqlDataAdapter(query, con) 
{ 
    var cmdParameters=sda.SelectCommand.Parameters; 
    parameters.AddWithValue("@tool",MoldText.Text); 
    parameters.AddWithValue("@part",IPNText.Text); 
    sda.Fill(dta); 
} 

あなたがのパラメータとして、アプリケーションの設定で接続文字列を格納することができますConnection Stringと入力すると、接続文字列のハードコーディングが回避されます。これにより、設定名

+0

'string'から 'System.Data.SqlClient.SqlCommand'へのクエリを変換できないと言われています – Carlos

+0

接続パラメータ –

+0

これを実行してもOKですが、ユーザーはツールや部品番号を入力することができます。入力したデータが間違っている場合は、エラーを出力します。 – Carlos

関連する問題