using System.Configuration string connectionstring=ConfigurationSettings.AppSettings["connect"];就行了。
全部代码如下: using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.Data.Common; using System.Data.OleDb; using System.Web.Configuration; using System.Configuration; namespace datalesson { /// <summary> /// WebForm1 的摘要说明。 /// </summary> public class WebForm1 : System.Web.UI.Page { protected System.Data.OleDb.OleDbDataAdapter oleDbDataAdapter1; protected System.Data.OleDb.OleDbCommand oleDbSelectCommand1; protected System.Data.OleDb.OleDbCommand oleDbInsertCommand1; protected System.Data.OleDb.OleDbCommand oleDbUpdateCommand1; protected System.Data.OleDb.OleDbCommand oleDbDeleteCommand1; protected System.Data.OleDb.OleDbConnection oleDbConnection2; protected datalesson.DataSet1 dataSet11; protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e) { string getconnectionstring =ConfigurationSettings.AppSettings["connect"]; OleDbConnection oleDbConnection2 = new OleDbConnection(getconnectionstring); OleDbDataAdapter oleDbDataAdapter1=new OleDbDataAdapter("select top 4 * from GSP order by id desc",oleDbConnection2); DataSet dataSet11=new DataSet(); oleDbDataAdapter1.Fill(dataSet11,"GSP"); DataGrid1.DataSource=dataSet11.Tables["GSP"].DefaultView; DataGrid1.DataBind(); } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); }
string connectionstring = ConfigurationSettings.AppSettings["connect"];
is c# not vb
string connectionstring=ConfigurationSettings.AppSettings["connect"];
正确了,可以下提示为:
找不到可安装的 ISAM
....
oleDbDataAdapter1.Fill(dataSet11,"GSP");这行错误
using System.Configuration
string connectionstring=ConfigurationSettings.AppSettings["connect"];就行了。
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.Common;
using System.Data.OleDb;
using System.Web.Configuration;
using System.Configuration;
namespace datalesson
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Data.OleDb.OleDbDataAdapter oleDbDataAdapter1;
protected System.Data.OleDb.OleDbCommand oleDbSelectCommand1;
protected System.Data.OleDb.OleDbCommand oleDbInsertCommand1;
protected System.Data.OleDb.OleDbCommand oleDbUpdateCommand1;
protected System.Data.OleDb.OleDbCommand oleDbDeleteCommand1;
protected System.Data.OleDb.OleDbConnection oleDbConnection2;
protected datalesson.DataSet1 dataSet11;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{ string getconnectionstring =ConfigurationSettings.AppSettings["connect"];
OleDbConnection oleDbConnection2 = new OleDbConnection(getconnectionstring);
OleDbDataAdapter oleDbDataAdapter1=new OleDbDataAdapter("select top 4 * from GSP order by id desc",oleDbConnection2);
DataSet dataSet11=new DataSet();
oleDbDataAdapter1.Fill(dataSet11,"GSP");
DataGrid1.DataSource=dataSet11.Tables["GSP"].DefaultView;
DataGrid1.DataBind();
} #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.oleDbDataAdapter1 = new System.Data.OleDb.OleDbDataAdapter();
this.oleDbDeleteCommand1 = new System.Data.OleDb.OleDbCommand();
this.oleDbConnection2 = new System.Data.OleDb.OleDbConnection();
this.oleDbInsertCommand1 = new System.Data.OleDb.OleDbCommand();
this.oleDbSelectCommand1 = new System.Data.OleDb.OleDbCommand();
this.oleDbUpdateCommand1 = new System.Data.OleDb.OleDbCommand();
this.dataSet11 = new datalesson.DataSet1();
((System.ComponentModel.ISupportInitialize)(this.dataSet11)).BeginInit();
//
// oleDbDataAdapter1
//
this.oleDbDataAdapter1.DeleteCommand = this.oleDbDeleteCommand1;
this.oleDbDataAdapter1.InsertCommand = this.oleDbInsertCommand1;
this.oleDbDataAdapter1.SelectCommand = this.oleDbSelectCommand1;
this.oleDbDataAdapter1.UpdateCommand = this.oleDbUpdateCommand1;
//
// oleDbDeleteCommand1
//
this.oleDbDeleteCommand1.Connection = this.oleDbConnection2;
//
// oleDbInsertCommand1
//
this.oleDbInsertCommand1.Connection = this.oleDbConnection2;
//
// oleDbSelectCommand1
//
this.oleDbSelectCommand1.Connection = this.oleDbConnection2;
//
// oleDbUpdateCommand1
//
this.oleDbUpdateCommand1.Connection = this.oleDbConnection2;
//
// dataSet11
//
this.dataSet11.DataSetName = "DataSet1";
this.dataSet11.Locale = new System.Globalization.CultureInfo("zh-CN");
this.dataSet11.Namespace = "http://www.tempuri.org/DataSet1.xsd";
this.Load += new System.EventHandler(this.Page_Load);
((System.ComponentModel.ISupportInitialize)(this.dataSet11)).EndInit(); }
#endregion
}
}
==>
Data Source要有空格
<appSettings>
<add key="connect" value="provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\china666.mdb "/>
</appSettings>
</configuration>页中
string connectionstring = new OleDbConnection(System.Configuration.ConfigurationSettings.AppSettings["data"]);
提示说:找不到文件 'C:\china666.mdb'。
2:如果为:<add key="connect" value="provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\china666.mdb"/>提示说:找不到文件 'C:\WINNT\china666.mdb'
可我的数据库放在项目文件的根目录下了,怎么才能指向它啊,