web.config文件中的配置为:
<connectionStrings>
<add name="connStr" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\1001\NewsWeb\test.MDB" providerName="System.Data.OleDb"/>
</connectionStrings>
//Error's static string connStr = ConfigurationManager.AppSettings["connStr"];
//static string connStr=System.Web.Configuration.WebConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
//static string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
static string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
Dbconnect mydbconn = new Dbconnect();
OleDbConnection odbCon = new OleDbConnection(connStr);
void bindNewsClassGrid()
{
string sql = "select id,newstype from NewsClass order by id";
GridView1.DataSource = mydbconn.dbConnectRtnDS(sql, odbCon);
GridView1.DataBind();
}
protected void b_newsLBAdd_Click(object sender, EventArgs e)
{
addNewsClass();
}
void addNewsClass()
{
if (!String.IsNullOrEmpty(TextBox1.Text))
{
string sql = "insert into NewsClass(newstype) values(" + "'" + TextBox1.Text + "')";
int i = mydbconn.dbConnectCmd(sql, odbCon, 1);
Response.Write("添加了" + i + "条记录!");
bindNewsClassGrid();
}
else
{
Response.Write("请输入新闻类型!");
}
}
void bindDropNewsCL()
{
string sql = "select id,newstype from NewsClass order by id";
DropDownList1.DataSource = mydbconn.dbConnectRtnDS(sql, odbCon);
DropDownList1.DataTextField = "newstype";
DropDownList1.DataValueField = "id";
DropDownList1.DataBind();
}
void addNewsInfo()
{
if (!String.IsNullOrEmpty(txtTitle.Text) && !String.IsNullOrEmpty(txtContent.Text))
{
System.DateTime currentTime = new System.DateTime();
currentTime = System.DateTime.Now;
//
string strT = currentTime.ToString("u").Substring(0, 19);
//strT 的格式化: yyyy-mm-dd hh:mm:ss
//string sdate = Calendar1.SelectedDate.ToString("u").Substring(0, 19);
string sql = "insert into newsinfo(newsclassid,newstitle,newscontent,userid,oprdate) "
+ "values(" + DropDownList1.SelectedValue
+ "," + "'" + txtTitle.Text + "'"
+ "," + "'" + txtContent.Text + "'"
+ "," + txtOper.Text
//+ "," + "#" + Calendar1.SelectedDate + "#)";
+ ",'" + sdate + "')";
Response.Write(sql);
int i = mydbconn.dbConnectCmd(sql, odbCon, 1);
Response.Write("添加了" + i + "条记录!");
Response.Write(Calendar1.SelectedDate.ToShortDateString());
//}
}
else
{
Response.Write("请输入新闻类型!");
}
}