//连接工程文件夹下目录为DataBase中question.mdb的数据库文件
public OleDbConnection OledCon()
{
string reportPath = Application.StartupPath.Substring(0, Application.StartupPath.Substring(0,Application.StartupPath.LastIndexOf("//")).LastIndexOf("//"));
reportPath += @"/DataBase/question.mdb";
string ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + reportPath;
OleDbConnection con = new OleDbConnection(ConStr);
return con;
}
//向数据库执行命令
public int xInsert(question_x tb_aut)//question_x一般为表的属性及方法
{
int intResult = 0;
OleDbCommand oledcmd = null;
OleDbConnection oledCon = null;
//OleDbDataReader oleRed = null;
try
{
//Connection_oleDB getCon = new Connection_oleDB();
//oledCon = getCon.OledCon();//如果上面方法在namespace Connection_oleDB
string reportPath = Application.StartupPath.Substring(0, Application.StartupPath.Substring(0,Application.StartupPath.LastIndexOf("//")).LastIndexOf("//"));
reportPath += @"/DataBase/question.mdb";
string ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + reportPath;
OleDbConnection oledCon = new OleDbConnection(ConStr);
oledCon.Open();
string strAdd = "insert into [表名] values ( ";
strAdd += "'" + tb_aut.x_ID + "','" + tb_aut.x_tm + "',";
strAdd += "'" + tb_aut.x_da + "','" + tb_aut.x_fz + "')";
oledcmd = new OleDbCommand(strAdd, oledCon);
intResult = oledcmd.ExecuteNonQuery();
return intResult;
}
catch (Exception ee)
{
MessageBox.Show(ee.Message.ToString());
return intResult;
}
}
//向数据库输入命令并得到返回值
public string get_index(string m_type, string m_id)
{
OleDbConnection conn = null;
//Connection_oleDB getCon = new Connection_oleDB();
//conn = getCon.OledCon();
//如果上面方法在namespace Connection_oleDB
string reportPath = Application.StartupPath.Substring(0, Application.StartupPath.Substring(0,Application.StartupPath.LastIndexOf("//")).LastIndexOf("//"));
reportPath += @"/DataBase/question.mdb";
string ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + reportPath;
OleDbConnection conn = new OleDbConnection(ConStr);
OleDbDataReader sdr1 = null;
conn.Open();
if (conn.State == ConnectionState.Open)
{
}
OleDbCommand cmd0 = new OleDbCommand();
cmd0.Connection = conn;
cmd0.CommandText = "select * from [表名] where [表中字段]='" + m_type + "' and ID='" + m_id.ToString() + "'";
sdr1 = cmd0.ExecuteReader();
string index = null;
while (sdr1.Read())
{
index = sdr1[1].ToString();
}
return index;
}