我们可以在ms access 2007中创建存储过程吗?

时间:2015-06-13 12:07:13

标签: ms-access stored-procedures

我们如何在MS Access 2007中创建存储过程。如果可以,那么应该如何为insert语句编写过程。

2 个答案:

答案 0 :(得分:1)

无法在MS Access 2007中创建存储过程。您需要Access 2013 - https://msdn.microsoft.com/en-us/library/office/ff845861(v=office.15).aspx

答案 1 :(得分:1)

此声明可用作示例

string queryText = @"
CREATE PROCEDURE Customer_Insert (pName  text(255), pAddress Text(255)) AS
    INSERT INTO Customers ( Name, Address )
    VALUES ([pName], [pAddress]);";

现在用OleDb执行非返回行命令的常用方法

using(OleDbConnection cn = new OleDbConnection(......))
using(OleDbCommand cmd = new OleDbCommand(queryText, cn))
{
    cn.Open();
    cmd.ExecuteNonQuery();
}

最后你可以用通常的方式调用它

using(OleDbConnection cn = new OleDbConnection(......))
using(OleDbCommand cmd = new OleDbCommand("Customer_Insert", cn))
{
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add("pName", OleDbType.VarWChar).Value = "yourCustomerName";
    cmd.Parameters.Add("pAddress", OleDbType.VarWChar).Value = "yourCustomerAddress";
    cn.Open();
    cmd.ExecuteNonQuery();
}

修改
只有在StoredProcedure不存在的情况下,才应创建存储过程(在Access接口中进行A.K.A.查询)。要发现数据库中是否已存在该过程,最简单的方法是执行以下操作

 using(OleDbConnection cn = new OleDbConnection(....));    
 { 
     con.Open();
     var schema = con.GetSchema("Procedures");
     DataRow[] r = schema.Select("PROCEDURE_NAME = 'Customer_Insert'");
     if(r.Length > 0)
         MessageBox.Show("Procedure 'Customer_Insert' already exists");
 }