public class DBConnection
{
public DBConnection()
{
strconn = ConfigurationManager.ConnectionStrings["ProgramManagementSystemConnectionString1"].ConnectionString;
}
private string strconn = "";
public List <Instructor> GetInstructors(out int rows)
{
string sql = "select fName, lName from instructors";
SqlConnection cn = new SqlConnection(strconn);
SqlCommand cmd = new SqlCommand(sql, cn);
SqlDataReader reader;
List<Instructor> instructorList= new List<Instructor>();
try
{
cn.Open();
reader = cmd.ExecuteReader();
while (reader.Read())
{
Instructor instructor = new Instructor();
instructor.FirstName = Convert .ToString ( reader[0]);
instructor.LastName = Convert .ToString ( reader[1]);
instructorList.Add(instructor);
}
rows = cmd.ExecuteNonQuery();
reader.Close();
}
catch (SqlException err)
{ }
finally
{
cn.Close();
}
return instructorList;
}
}
然后UI中
protected void Button3_Click(object sender, EventArgs e)
{
int rows = 0;
DBConnection dbCon = new DBConnection();
GridView1.DataSource = dbCon.GetInstructors(out rows);
GridView1.DataBind();
Label3.Text = rows.ToString();
}错误显示:
Compiler Error Message: CS0177: The out parameter 'rows' must be assigned to before control leaves the current methodSource Error: Line 54: }
Line 55:
Line 56: return instructorList;
Line 57:
Line 58:
Source File: d:\Our Documents\Visual Studio 2005\WebSites\WebSite3\App_Code\DBConnection.cs Line: 56 这是怎么回事呢?谢谢。另外,得出一个sql语句所选择的记录数,是不是用out参数比较好,还是有其他更好的办法。
{
public DBConnection()
{
strconn = ConfigurationManager.ConnectionStrings["ProgramManagementSystemConnectionString1"].ConnectionString;
}
private string strconn = "";
public List <Instructor> GetInstructors(out int rows)
{
string sql = "select fName, lName from instructors";
SqlConnection cn = new SqlConnection(strconn);
SqlCommand cmd = new SqlCommand(sql, cn);
SqlDataReader reader;
List<Instructor> instructorList= new List<Instructor>();
try
{
cn.Open();
reader = cmd.ExecuteReader();
while (reader.Read())
{
Instructor instructor = new Instructor();
instructor.FirstName = Convert .ToString ( reader[0]);
instructor.LastName = Convert .ToString ( reader[1]);
instructorList.Add(instructor);
}
rows = cmd.ExecuteNonQuery();
reader.Close();
}
catch (SqlException err)
{ }
finally
{
cn.Close();
}
return instructorList;
}
}
然后UI中
protected void Button3_Click(object sender, EventArgs e)
{
int rows = 0;
DBConnection dbCon = new DBConnection();
GridView1.DataSource = dbCon.GetInstructors(out rows);
GridView1.DataBind();
Label3.Text = rows.ToString();
}错误显示:
Compiler Error Message: CS0177: The out parameter 'rows' must be assigned to before control leaves the current methodSource Error: Line 54: }
Line 55:
Line 56: return instructorList;
Line 57:
Line 58:
Source File: d:\Our Documents\Visual Studio 2005\WebSites\WebSite3\App_Code\DBConnection.cs Line: 56 这是怎么回事呢?谢谢。另外,得出一个sql语句所选择的记录数,是不是用out参数比较好,还是有其他更好的办法。
若没有行受影响会返回-1,否则就是实际影响行数,
如果返回-1也就说明插入或更新(如果是插入或更新)并没有真正的执行
查询可使用可断式连接,然后填充到数据集中(DataAdapter)