→.NETプログラミング

→データベース関連

#contents

*Tips [#q0cfd467]
-Windows認証で接続したいとき
--接続文字列に Integrated Security=SSPI を追加する
--http://www.microsoft.com/japan/msdn/sqlserver/columns/ADONET/ADONET2.aspx

-[[バイナリデータの入出力>http://support.microsoft.com/default.aspx?scid=kb;ja;309158]]

-SqlCommandのパラメータとOleDbCommandのパラメータの渡し方は微妙に違うので注意
--SqlCommandの場合
---プレースホルダは "@xxx"とし、SqlParameterの名前にも"@xxx"を渡す
--OleDbCommandの場合
---プレースホルダは "?" とし、OleDbParameterの名前にはフィールド名を渡す
--OleDbCommandに対してSqlCommandのやり方でパラメータを渡すと「''スカラ変数@xxxを宣言してください''」というメッセージのエラーになる。

-DataSetのDataRowからカラム名(列名、フィールド名)を取得するには
--row.Table.Columns[i].ColumnNameを参照する

-SqlConnectionで接続プールをオフにするには接続文字列に "Pooling=false;"を追加する。
--SqlConnectionはOleDbConnectionとは微妙に違うので注意

-ADO.NETの接続文字列の簡単な作り方
--新規テキストファイルを作成し、拡張子を「udl」(OLE DB Universal Data Link)に変更してダブルクリック。
後は、ウィザードに従って入力していけば、ファイルの中に接続文字列が生成されます。
--ソース:http://www.vacant-eyes.jp/Tips/tadonet.html

-更新系SQLを文字列編集の際の注意
--こういうコードを書いたらダメ
 void LogUserName(SqlConnection conn, string userName) {
   string sqlText = "insert user_names values('" + userName + "')";
   SqlCommand cmd = new SqlCommand(sqlText, conn);
   cmd.ExecuteNonQuery();
 }
--userNameに何が入ってくるかわからないから(いわゆるSQLインジェクションの危険性)
--例えば ''名前','その隣の項目に入れたい値'' という文字列が来るとか。
--パラメータを使うこと
 void LogUserName(SqlConnection conn, string userName) {
   string sqlText = "insert user_names values(@n)";
   SqlCommand cmd = new SqlCommand(sqlText, conn);
   SqlParameter p = cmd.Parameters.Add("@n", SqlDbType.VarChar, userName.Length);
   p.Value = userName;
   cmd.ExecuteNonQuery();
 }

-SqlCommandの例外を拾うときはSqlExceptionでやった方が多少細かく情報が取れます


*パフォーマンス [#k540827e]
-[[Connection Pooling at a Glance>http://www.codeproject.com/useritems/ADONET_ConnectionPooling.asp]]

-[[ADO.NETのパフォーマンス調査:http://www.devx.com/vb2themax/Article/19887]]
--SQLClientDataReaderが最速との結果

-[[ADO.NET パフォーマンスの向上@MSDN:http://www.microsoft.com/japan/msdn/enterprise/pag/scalenetchapt12.asp]]

*SQL Serverに接続すると General network error.  Check your network documentation. というエラーになる [#x1999142]
-OLE DBで無効なコネクションがプールされることから起きる模様
-こちらを参照:http://support.microsoft.com/?scid=kb%3Ben-us%3B229564&x=15&y=8
-CAUSE
 In the current design, after an application role is enabled on a client connection to SQL Server, 
 you cannot reset the security context of that connection. Therefore, when the user ends 
 the SQL Server session and disconnects from the server, the session is not reusable. 
 However, OLE DB resource pooling returns the closed connection to the pool, 
 and the error occurs when that connection is reused and the client application tries to reset 
 the connection's security context by calling sp_setapprole again.

-WORKAROUND
 The only available workaround is to disable OLE DB Resource Pooling, which ADO uses by default.
 You can do this by adding "OLE DB Services = -2" to the ADO Connection string, as shown here:

-サンプル
 'For SQLOLEDB provider
 'strConnect = "Provider=SQLOLEDB;server=SQL7Web;OLE DB Services = -2;uid=AppUser;pwd=AppUser;
  initial catalog=northwind"
 
 ' For MSDASQL provider
 'strConnect = "DSN=SQLNWind;UID=Test;PWD=Test; OLE DB Services= -2"
 
Pooling can be disabled for the SQL Server .Net Data Provider by adding 
"Pooling=False" to the connection  string.

-ただし、SQLConnectionクラスを使ってる場合、"OLE DB Services"というパラメータ自体受け付けてくれない。"Pooling=false"というのがそれに当たる模様


*その他 [#fffa5352]
-[[ADO.NET 2.0の接続文字列の管理問題>http://www.digitaldj.jp/2007/07/16_100000.html]] 2007.7.16
--.NET 2.0では接続文字列の管理場所がappSettings要素からconnectionStrings要素へ移動してしまったので、user.configで環境ごとの設定を持つことができなくなってしまいました。

トップ   編集 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS