数据访问层
使用例子:
using Microsoft.ApplicationBlocks.Data;
List paramlist = new List();
paramlist.Add(new SqlParameter("@isMonitor", isMonitorList));
paramlist.Add(new SqlParameter("@status", status));
return Convert.ToInt32( SqlHelper.ExecuteScalar((SqlConnection)conn,sql,paramlist.ToArray()) );
paramlist.Add(new SqlParameter("@isMonitor", isMonitorList));
paramlist.Add(new SqlParameter("@status", status));
return Convert.ToInt32( SqlHelper.ExecuteScalar((SqlConnection)conn,sql,paramlist.ToArray()) );
值得注意的是:在数据访问层中,传递参数顺序一定要和存储过程写的参数顺序一模一样,否则遇到类型不一样的情况会报错,编译不通过,不清楚为什么?下面的例子,如果1.6行顺序变的话,就出错了
或者 // SqlParameter[] param = {
1 // new SqlParameter("@usercode",user.userCode ),
2 // new SqlParameter("@userName", user.userName),
3 //new SqlParameter("@password", user.Password),
4// new SqlParameter("@description", user.description),
5 // new SqlParameter("@actived", user.Actived),
6// new SqlParameter("@workgroupID", user.workgroupID)
1 // new SqlParameter("@usercode",user.userCode ),
2 // new SqlParameter("@userName", user.userName),
3 //new SqlParameter("@password", user.Password),
4// new SqlParameter("@description", user.description),
5 // new SqlParameter("@actived", user.Actived),
6// new SqlParameter("@workgroupID", user.workgroupID)
// };
使用的实例子
private void cmdSample1_Click(object sender, System.EventArgs e)
{
// SqlDataReader that will hold the returned results
SqlDataReader dr = null;
// SqlConnection that will be used to execute the sql commands
SqlConnection connection = null;
{
// SqlDataReader that will hold the returned results
SqlDataReader dr = null;
// SqlConnection that will be used to execute the sql commands
SqlConnection connection = null;
try
{
try
{
connection = GetConnection(txtConnectionString.Text);
}
catch
{
MessageBox.Show("The connection with the database can磘 be established", "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
catch(Exception ex)
{
}
finally
{
if(dr != null)
((IDisposable)dr).Dispose();
{
try
{
connection = GetConnection(txtConnectionString.Text);
}
catch
{
MessageBox.Show("The connection with the database can磘 be established", "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
catch(Exception ex)
{
}
finally
{
if(dr != null)
((IDisposable)dr).Dispose();
if(connection != null)
connection.Dispose();
}
}
connection.Dispose();
}
}
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/23109131/viewspace-687478/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/23109131/viewspace-687478/