数据访问层:
本层主要包括有下列成员:
- ParamData 结构
- StoredProcedure 类
- StoredProcedureCollection 类
- Execute 类
其中ParamData 含有存储过程参数的名称,值,以及各自的数据类型.
1
struct
ParamData
2 {
3public string pName,pValue;
4public SqlDbType pDataType;
5public ParamData(string pName,SqlDbType pDataType,string pValue)
6{
7this.pName=pName;
8this.pDataType=pDataType;
9this.pValue=pValue;
10}
11}
12
2 {
3public string pName,pValue;
4public SqlDbType pDataType;
5public ParamData(string pName,SqlDbType pDataType,string pValue)
6{
7this.pName=pName;
8this.pDataType=pDataType;
9this.pValue=pValue;
10}
11}
12
StoredProcedure 含有SetParam 和 Getparam两大方法,他们分别用来设置和获取存储过程的参数列表的.
public
void
SetParam(
string
pName,SqlDbType pDataType,
string
pValue)
{
ParamData pData=new ParamData(pName,pDataType,pValue);
// adding to array list sParams.
sParams.Add(pData);
}
public ArrayList GetParams()
{
if (!(sParams==null))
{
return sParams;
}
else
{
return null;
}
}
{
ParamData pData=new ParamData(pName,pDataType,pValue);
// adding to array list sParams.
sParams.Add(pData);
}
public ArrayList GetParams()
{
if (!(sParams==null))
{
return sParams;
}
else
{
return null;
}
}
StoredProcedureCollection类 是一个集合类, 它含有多个Stored Procedure以及Add和Remove,Item方法.
public
void
add(StoredProcedure value)
{
List.Add(value);
}
public void Remove( int index)
{
if (index > Count - 1 || index < 0)
{
Console.WriteLine("No data to remove");
}
else
{
List.RemoveAt(index);
}
}
public StoredProcedure Item( int Index)
{
return (StoredProcedure) List[Index];
}
{
List.Add(value);
}
public void Remove( int index)
{
if (index > Count - 1 || index < 0)
{
Console.WriteLine("No data to remove");
}
else
{
List.RemoveAt(index);
}
}
public StoredProcedure Item( int Index)
{
return (StoredProcedure) List[Index];
}
Execute 类 包括有一个ExecuteSps 静态方法,它主要执行来自StoredProcedureCollection类的存储过程
public
static
bool
ExecuteSps(StoredProcedureCollection spCollection,SqlConnection Connection)
{
try
{
foreach (StoredProcedure spData in spCollection)
{
SqlCommand cmd = new SqlCommand();
int i = 0 ;
if (Connection.State != ConnectionState.Open)
Connection.Open();
cmd.Connection = Connection; cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = spData.ProcName;
IEnumerator myEnumerator = spData.GetParams().GetEnumerator();
while (myEnumerator.MoveNext())
{
ParamData pData = (ParamData)myEnumerator.Current;
cmd.Parameters.Add(pData.pName,pData.pDataType);
cmd.Parameters[i].Value = pData.pValue;
i = i + 1 ;
}
cmd.ExecuteNonQuery();
}
return true ;
}
catch (Exception exc)
{
return false ;
}
}
{
try
{
foreach (StoredProcedure spData in spCollection)
{
SqlCommand cmd = new SqlCommand();
int i = 0 ;
if (Connection.State != ConnectionState.Open)
Connection.Open();
cmd.Connection = Connection; cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = spData.ProcName;
IEnumerator myEnumerator = spData.GetParams().GetEnumerator();
while (myEnumerator.MoveNext())
{
ParamData pData = (ParamData)myEnumerator.Current;
cmd.Parameters.Add(pData.pName,pData.pDataType);
cmd.Parameters[i].Value = pData.pValue;
i = i + 1 ;
}
cmd.ExecuteNonQuery();
}
return true ;
}
catch (Exception exc)
{
return false ;
}
}
更多实现细节,见 DataAccessLayer.ZIP
应用层:
在应用层我们添加对DataAccessLayer.dll的引用后,我们就可以随意地在需要的地方调用数据层的功能.这样做的一大优势就是:我们可以随着存储过程需求的变化,来添加和删除任意多的参数.
1
private
void
button1_Click(
object
sender, System.EventArgs e)
2 {
3SqlConnection connection=new SqlConnection();
4//change this connect string as per your environment
5string connectString="Persist Security Info=False;Integrated Security=SSPI;database=DB1;server=Server2;Connect Timeout=60";
6connection.ConnectionString=connectString;
7if (connection.State!=ConnectionState.Open)
8connection.Open();
9DataAccessLayer.StoredProcedureCollection spCollection=new DataAccessLayer.StoredProcedureCollection();
10DataAccessLayer.StoredProcedure spData=new DataAccessLayer.StoredProcedure();
11spData.ProcName=txtSpName.Text;
12spData.SetParam(txtParam1.Text,SqlDbType.VarChar,txtParamValue1.Text);
13spData.SetParam(txtParam2.Text,SqlDbType.VarChar,txtParamValue2.Text);
14spCollection.add(spData);
15if (DataAccessLayer.Execute.ExecuteSps(spCollection,connection))
16MessageBox.Show("Successfully executed");
17}
18 }
19 catch (Exception exc)
20 {
21return false;
22}
23 }
更多实现细节 见
CallingAPP.zip
2 {
3SqlConnection connection=new SqlConnection();
4//change this connect string as per your environment
5string connectString="Persist Security Info=False;Integrated Security=SSPI;database=DB1;server=Server2;Connect Timeout=60";
6connection.ConnectionString=connectString;
7if (connection.State!=ConnectionState.Open)
8connection.Open();
9DataAccessLayer.StoredProcedureCollection spCollection=new DataAccessLayer.StoredProcedureCollection();
10DataAccessLayer.StoredProcedure spData=new DataAccessLayer.StoredProcedure();
11spData.ProcName=txtSpName.Text;
12spData.SetParam(txtParam1.Text,SqlDbType.VarChar,txtParamValue1.Text);
13spData.SetParam(txtParam2.Text,SqlDbType.VarChar,txtParamValue2.Text);
14spCollection.add(spData);
15if (DataAccessLayer.Execute.ExecuteSps(spCollection,connection))
16MessageBox.Show("Successfully executed");
17}
18 }
19 catch (Exception exc)
20 {
21return false;
22}
23 }