ACCESS中存储过程实际上就是ACCESS中创建的Update、Insert、Delete语句。
C#中执行ACCESS中存储过程示例如下:
库名D:\db_test.accdb,表名:tb_test(Id []自动编号],RunCount long[默认为 0],test text(10))
Update 无参语句:名称RunAddOne
UPDATE tb_test SET tb_test.RunCount = [tb_test]![RunCount]+1
WHERE (([tb_test]![Id]=1)); 程序每启动一次加一
INSERT 语句:名称tb_testInsert
PARAMETERS test Text ( 10 ); //ACCESS不支持写为@test
INSERT INTO tb_test(test)
SELECT [test] AS test;
private void Updatebutton_Click(object sender, EventArgs e)
{
OleDbConnection conn = null;
string strConnection = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\db_test.accdb;Persist Security Info=True;Jet OLEDB:Database Password=123456";
conn = new OleDbConnection(strConnection);
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "RunAddOne";
conn.Open();
try
{
int rowCount = cmd.ExecuteNonQuery();
if (rowCount == 1)
{
MessageBox.Show("更新成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("更新失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
conn.Close();
MessageBox.Show(ex.Message);
}
}
private void Addbutton_Click(object sender, EventArgs e)
{
OleDbConnection conn = null;
string strConnection = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\db_test.accdb;Persist Security Info=True;Jet OLEDB:Database Password=123456";
conn = new OleDbConnection(strConnection);
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "tb_testInsert" + " " + this.textBox1.Text;//加空格很重要
conn.Open();
try
{
int rowCount = cmd.ExecuteNonQuery();
if (rowCount == 1)
{
MessageBox.Show("添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("添加失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
conn.Close();
MessageBox.Show(ex.Message);
}
}
其实也比较简单啊!