111,126
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
namespace Fly.ESTown.Web
{
public partial class WebForm1 : System.Web.UI.Page
{
public static string connectionString = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" +
System.Web.HttpContext.Current.Server.MapPath("~/App_Data/enstom.mdb");
protected void Page_Load(object sender, EventArgs e)
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
try
{
connection.Open();
string strLableText = "DataSource: " + connection.DataSource + "</br>Provider:" + connection.Provider;
this.Label1.Text = strLableText;
}
catch (Exception ex)
{
this.Label1.Text = ex.Message + ex.Source + ex.InnerException + ex.StackTrace + ex.TargetSite;
}
// The connection is automatically closed when the
// code exits the using block.
}
}
}
}
...
string sql = "SELECT log_level, log_time, log_content, log_type, module_name, camera_id, user_name FROM log WHERE server_id=?";
OleDbParameter[] parameters = new OleDbParameter[1];
parameters[0] = new OleDbParameter("@server_id", OleDbType.VarChar);
parameters[0].Value = serverId;
...
string appPath = Application.StartupPath + "\\";
...
private static string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + appPath + "DB\\log.mdb";
...
public static DataSet ExecuteQuery(string sql, OleDbParameter[] parameters)
{
Console.WriteLine(sql);
Console.WriteLine(connString);
using (OleDbConnection connection = new OleDbConnection(connString))
{
DataSet ds = new DataSet();
try
{
connection.Open();// <-这里出现异常
OleDbDataAdapter da = new OleDbDataAdapter(sql, connection);
if (parameters != null)
da.SelectCommand.Parameters.AddRange(parameters);
da.Fill(ds, "ds");
return ds;
}
catch (Exception)
{
return null;
}
finally
{
connection.Close();
}
}
}