以Oracle為例,其它數據庫僅改變數據庫對象就可以。下面是帶參數的,不帶參數的話paramete為null即可。
參數對應
public static int UploadStartCloseSoftInfo(string[] args)
{
OracleParameter parameter1 = new OracleParameter(參數名1, OracleType.類型);
parameter1.Value =類型轉換(args[0]);
OracleParameter parameter2 = new OracleParameter(參數名2, OracleType.類型);
parameter2.Value =類型轉換(args[1]);
... ...
OracleParameter parametern = new OracleParameter(參數名n, OracleType.類型);
parametern.Value =類型轉換(args[n-1]);
OracleParameter[] parameter
= {parameter1,parameter2,...,parametern};
return SDProvider.RunProcedure(存儲過程名, parameter);
}
}
}
public static int UploadStartCloseSoftInfo(string[] args)
{
OracleParameter parameter1 = new OracleParameter(參數名1, OracleType.類型);
parameter1.Value =類型轉換(args[0]);
OracleParameter parameter2 = new OracleParameter(參數名2, OracleType.類型);
parameter2.Value =類型轉換(args[1]);
... ...
OracleParameter parametern = new OracleParameter(參數名n, OracleType.類型);
parametern.Value =類型轉換(args[n-1]);
OracleParameter[] parameter
= {parameter1,parameter2,...,parametern};
return SDProvider.RunProcedure(存儲過程名, parameter);
}
}
}
執行
public class SDProvider
{
public static string GetConnectionString()
{
string conStr =連接數據庫字符串;
return conStr ;
}
public static int RunProcedure(string prName, IDataParameter[] parameters)
{
try
{
using(OracleConnection connection=
new OracleConnection(GetConnectionString()))
{
connection.Open();
OracleCommand command =
BuildQueryCommand(connection, storedProcName, parameters);
return command.ExecuteNonQuery();
}
}
catch
{
}
return 0;
}
private static OracleCommand BuildQueryCommand
(OracleConnection connection,
string storedProcName,
IDataParameter[]parameters)
{
OracleCommand command = new OracleCommand(storedProcName, connection);
command.CommandType = CommandType.StoredProcedure;
if(parameters!=null)
{
foreach (OracleParameter parameter in parameters)
{
command.Parameters.Add(parameter);
}
}
return command;
}
}
public class SDProvider
{
public static string GetConnectionString()
{
string conStr =連接數據庫字符串;
return conStr ;
}
public static int RunProcedure(string prName, IDataParameter[] parameters)
{
try
{
using(OracleConnection connection=
new OracleConnection(GetConnectionString()))
{
connection.Open();
OracleCommand command =
BuildQueryCommand(connection, storedProcName, parameters);
return command.ExecuteNonQuery();
}
}
catch
{
}
return 0;
}
private static OracleCommand BuildQueryCommand
(OracleConnection connection,
string storedProcName,
IDataParameter[]parameters)
{
OracleCommand command = new OracleCommand(storedProcName, connection);
command.CommandType = CommandType.StoredProcedure;
if(parameters!=null)
{
foreach (OracleParameter parameter in parameters)
{
command.Parameters.Add(parameter);
}
}
return command;
}
}
注意:
1、存儲過程名正確。
2、參數名1,參參數名2,這些的名字要和存儲過程中的參數名一樣,順序和類型均要對應一致。
摘自 白楊樹