之前的DLL链接库,只能返回一个值
因此决定改进一下
用数组来返回多个结果
更改结果如下
public static string[] ExecnonQuery(string cmdtext, CommandType ct,SqlParameter[] param)
{
SqlConnection conn = new SqlConnection(con);
SqlCommand comm = new SqlCommand(cmdtext, conn);
comm.CommandType = ct;
if (param != null)
{
foreach (SqlParameter sp in param)
{
comm.Parameters.Add(sp);
}
}
try
{
conn.Open();
string[] array=new string[2];
SqlDataReader sr = comm.ExecuteReader();
if (sr.Read())
{
array[0] = sr["ID"].ToString();
array[1] = sr["Sname"].ToString();
conn.Close();
}
return array;
}
catch (Exception ex)
{
throw ex;
}
finally
{
conn.Close();
}
}
调用DLL的命令也更改为如下
private void button2_Click(object sender, EventArgs e)
{
string cmdtext = ("proc_student");
SqlParameter[] param = new SqlParameter[1];
param[0] = new SqlParameter("@ID", SqlDbType.Int, 10);
param[0].Value = tid2.Text;
string[] array = ClassLibrary1.Class1.ExecnonQuery(cmdtext, CommandType.StoredProcedure, param);
tid2.Text = array[0].ToString();
tname2.Text = array[1].ToString();
}