程序部分
public
class DataAccess
{
SqlConnection Conn;
SqlDataAdapter DA = new SqlDataAdapter();
SqlCommandBuilder CB;
DataTable DT;
public SqlConnection OpenConnection()
{
Conn = new SqlConnection("data source=172.27.26.61;database=M_shop;User ID=webplus;Password=webplus");
return Conn;
}
public Array CallStoredProcedure(string[,] MyINPram, string[,] MyOUTPram, string MySPName)
{
string Result;
Conn = OpenConnection();
SqlCommand CmdSp = new SqlCommand();
CmdSp.Connection = Conn;
= MySPName.ToString();
= CommandType.StoredProcedure;
//input parameters
for (int i = 0; i < MyINPram.Length / 2; i++)
{
CmdSp.Parameters.AddWithValue(MyINPram[i, 0], MyINPram[i, 1]);
}
//outparameters
for (int i = 0; i < MyOUTPram.Length / 2; i++)
{
if (MyOUTPram[i, 1] == "SqlDbType.VarChar")
{
CmdSp.Parameters.Add(MyOUTPram[i, 0], SqlDbType.VarChar, 150);
}
else
{
CmdSp.Parameters.Add(MyOUTPram[i, 0], SqlDbType.Int, 4);
//CmdSp.Parameters.Add(MyOUTPram[i, 0], MyOUTPram[i,1]);
}
CmdSp.Parameters[MyOUTPram[i, 0]].Direction = ParameterDirection.Output;
}
Conn.Open();
CmdSp.ExecuteNonQuery();
Conn.Close();
string[,] Temparray = new string[MyOUTPram.Length / 2, 2];
for (int i = 0; i < MyOUTPram.Length / 2; i++)
{
Result = Convert.ToString(CmdSp.Parameters[MyOUTPram[i, 0]].Value);
//Result = (string)CmdSp.Parameters["@Msg"].Value;
Temparray[i, 0] = MyOUTPram[i, 0];
Temparray[i, 1] = Result;
}
return Temparray;
}
public DataTable CallStoredProcedure(string MySPName)
{
Conn = OpenConnection();
SqlCommand cm = new SqlCommand(MySPName, Conn);
= CommandType.StoredProcedure;
DA.SelectCommand = cm;
DT = new DataTable();
DA.Fill(DT);
return DT;
}
}