ASP.NET C# データベースアクセス

  OLEDB



null のデータを myReader.GetString で入力しようとすると、例外が発生するので、
myReader.IsDBNull で前もって検査する必要があります。

Microsoft へのリンク ( GetString )

  
<%@ Page CodePage="932" Language="C#" Debug="true" %>
<%@ Import Namespace="System.Data.OleDb" %>

<SCRIPT language="C#" runat=server>


void Page_Load(Object sender, EventArgs e) {

	Response.Charset = "shift_jis";
	Response.Cache.SetExpires(DateTime.Parse("May 31,2000 6:00:00PM"));

	string myConnectionString;

	// 接続文字列作成
	myConnectionString = "Provider=SQLOLEDB;";
	myConnectionString += "Data Source=xxxxxx;";
	myConnectionString += "Initial Catalog=yyyyyyyy;";
	myConnectionString += "User ID=user;";
	myConnectionString += "Password=pass;";

	// 接続準備
	OleDbConnection myConnection = new OleDbConnection(myConnectionString);

	// コマンド
	string myQuery = "select * from [stores]";
	// コマンド準備
	OleDbCommand myCommand = new OleDbCommand(myQuery);
	// コマンドを使用する接続用インスタンスを関係付ける
	myCommand.Connection = myConnection;
	// 接続
	myConnection.Open();

	// レコード取得用のオブジェクト
	OleDbDataReader myReader;
	// レコード取得要のオブジェクトを取得
	myReader = myCommand.ExecuteReader();

	// データ取得用の変数
	string str;
	int fld;

	// 全てのレコードを取得
	while( myReader.Read() ) {

		// 列の番号
		fld = myReader.GetOrdinal("店名");
		// 列の文字列値
		if ( myReader.IsDBNull( fld ) ) {
			str = "null";
		}
		else {
			str = myReader.GetString(fld);
		}
		Response.Write( str + "<br>" );
	
	}

	// 接続解除
	myConnection.Close();

}

</SCRIPT>
  




  HTTPヘッダ



  
HTTP/1.1 200 OK
Server: Microsoft-IIS/5.0
Date: Wed, 31 Jan 2007 13:18:29 GMT
X-Powered-By: ASP.NET
X-AspNet-Version: 1.1.4322
Cache-Control: private
Expires: Wed, 31 May 2000 09:00:00 GMT
Content-Type: text/html; charset=shift_jis
Content-Length: 5400
  



  web.config

  
<configuration>
	<system.web>
		<customErrors mode="Off"/>
	</system.web>
</configuration>
  

  
<customErrors mode="Off"/>
カスタム エラーが無効であることを指定します。これにより、詳細なエラーを表示できます

.aspx に対して、エラーのリダイレクトをする場合は以下のように指定します
  

  
<configuration>
	<system.web>
		<customErrors mode="On">
			<error statusCode="404"
				redirect="not_found.html" />
		</customErrors>
	</system.web>
</configuration>
  



  実際の展開コード

  
行 1:    //------------------------------------------------------------------------------
行 2:    // <autogenerated>
行 3:    //     This code was generated by a tool.
行 4:    //     Runtime Version: 1.1.4322.2300
行 5:    //
行 6:    //     Changes to this file may cause incorrect behavior and will be lost if 
行 7:    //     the code is regenerated.
行 8:    // </autogenerated>
行 9:    //------------------------------------------------------------------------------
行 10:   
行 11:   namespace ASP {
行 12:       using System;
行 13:       using System.Collections;
行 14:       using System.Collections.Specialized;
行 15:       using System.Configuration;
行 16:       using System.Text;
行 17:       using System.Text.RegularExpressions;
行 18:       using System.Web;
行 19:       using System.Web.Caching;
行 20:       using System.Web.SessionState;
行 21:       using System.Web.Security;
行 22:       using System.Web.UI;
行 23:       using System.Web.UI.WebControls;
行 24:       using System.Web.UI.HtmlControls;
行 25:       
行 26:       #line 2 "e:\.....\ccp_01.aspx"
行 27:       using System.Data.OleDb;
行 28:       
行 29:       #line default
行 30:       #line hidden
行 31:       
行 32:       
行 33:       [System.Runtime.CompilerServices.CompilerGlobalScopeAttribute()]
行 34:       public class ccp_01_aspx : System.Web.UI.Page, System.Web.SessionState.IRequiresSessionState {
行 35:           
行 36:           private static int __autoHandlers;
行 37:           
行 38:           private static bool __initialized = false;
行 39:           
行 40:           private static System.Collections.ArrayList __fileDependencies;
行 41:           
行 42:           
行 43:           #line 4 "e:\.....cp\ccp_01.aspx"
行 44:           
行 45:   
行 46:   
行 47:   void Page_Load(Object sender, EventArgs e) {
行 48:   
行 49:   	Response.Charset = "shift_jis";
行 50:   	Response.Cache.SetExpires(DateTime.Parse("May 31,2000 6:00:00PM"));
行 51:   
行 52:   	string myConnectionString;
行 53:   
行 54:   	// 接続文字列作成
行 55:   	myConnectionString = "Provider=SQLOLEDB;";
行 56:   	myConnectionString += "Data Source=xxxxxx;";
行 57:   	myConnectionString += "Initial Catalog=yyyyyy;";
行 58:   	myConnectionString += "User ID=user;";
行 59:   	myConnectionString += "Password=pass;";
行 60:   
行 61:   	// 接続準備
行 62:   	OleDbConnection myConnection = new OleDbConnection(myConnectionString);
行 63:   
行 64:   	// コマンド
行 65:   	string myQuery = "select * from [stores]";
行 66:   	// コマンド準備
行 67:   	OleDbCommand myCommand = new OleDbCommand(myQuery);
行 68:   	// コマンドを使用する接続用インスタンスを関係付ける
行 69:   	myCommand.Connection = myConnection;
行 70:   	// 接続
行 71:   	myConnection.Open();
行 72:   
行 73:   	// レコード取得用のオブジェクト
行 74:   	OleDbDataReader myReader;
行 75:   	// レコード取得要のオブジェクトを取得
行 76:   	myReader = myCommand.ExecuteReader();
行 77:   
行 78:   	// データ取得用の変数
行 79:   	string str;
行 80:   	int fld;
行 81:   
行 82:   	// 全てのレコードを取得
行 83:   	while( myReader.Read() ) {
行 84:   
行 85:   		// 列の番号
行 86:   		fld = myReader.GetOrdinal("店名");
行 87:   		// 列の文字列値
行 88:   		if ( myReader.IsDBNull( fld ) ) {
行 89:   			str = "null";
行 90:   		}
行 91:   		else {
行 92:   			str = myReader.GetString(fld);
行 93:   		}
行 94:   		Response.Write( str + "<br>" );
行 95:   	
行 96:   	}
行 97:   
行 98:   	// 接続解除
行 99:   	myConnection.Close();
行 100:  
行 101:  }
行 102:  
行 103:  
行 104:          #line default
行 105:          #line hidden
行 106:          
行 107:          public ccp_01_aspx() {
行 108:              System.Collections.ArrayList dependencies;
行 109:              if ((ASP.ccp_01_aspx.__initialized == false)) {
行 110:                  dependencies = new System.Collections.ArrayList();
行 111:                  dependencies.Add("e:\\.....\\ccp_01.aspx");
行 112:                  ASP.ccp_01_aspx.__fileDependencies = dependencies;
行 113:                  ASP.ccp_01_aspx.__initialized = true;
行 114:              }
行 115:              this.Server.ScriptTimeout = 30000000;
行 116:          }
行 117:          
行 118:          protected override int AutoHandlers {
行 119:              get {
行 120:                  return ASP.ccp_01_aspx.__autoHandlers;
行 121:              }
行 122:              set {
行 123:                  ASP.ccp_01_aspx.__autoHandlers = value;
行 124:              }
行 125:          }
行 126:          
行 127:          protected System.Web.HttpApplication ApplicationInstance {
行 128:              get {
行 129:                  return ((System.Web.HttpApplication)(this.Context.ApplicationInstance));
行 130:              }
行 131:          }
行 132:          
行 133:          public override string TemplateSourceDirectory {
行 134:              get {
行 135:                  return "/ccp";
行 136:              }
行 137:          }
行 138:          
行 139:          private void __BuildControlTree(System.Web.UI.Control __ctrl) {
行 140:          }
行 141:          
行 142:          protected override void FrameworkInitialize() {
行 143:              this.__BuildControlTree(this);
行 144:              this.FileDependencies = ASP.ccp_01_aspx.__fileDependencies;
行 145:              this.CodePage = 932;
行 146:              this.EnableViewStateMac = true;
行 147:              this.Request.ValidateInput();
行 148:          }
行 149:          
行 150:          public override int GetTypeHashCode() {
行 151:              return 5381;
行 152:          }
行 153:      }
行 154:  }
行 155:  
  










  infoboard   管理者用   
このエントリーをはてなブックマークに追加





フリーフォントWEBサービス
SQLの窓WEBサービス

SQLの窓フリーソフト

素材

一般WEBツールリンク

SQLの窓

フリーソフト

JSライブラリ