//ADO.net需要引入的命名空间
using System.Data.SqlClient
using System.Data
//ConfigurationManager类需要引入以下命名空间
using System.Configuration
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
//响应"查询学生表"按钮-演示利用SqlDataReader类遍历表记录
protected void btn1_Click(object sender, EventArgs e)
{
//获取数据库连接字符串(ConfigurationManager类需要引入System.Configuration的命名空间)
string sqlConnectStr = ConfigurationManager.ConnectionStrings["SchoolConnectionString"].ConnectionString
//创建数据库连接对象(SqlConnection)
SqlConnection myConnection = new SqlConnection(sqlConnectStr)
//创建数据库命令对象(SqlCommand)
SqlCommand myCommand = new SqlCommand("select * from Student", myConnection)
//打开数据库连接
myConnection.Open()
//创建数据读取对象(注意:ExecuteReader()方法获取的数据只读,而且只能向后移动记录)
SqlDataReader myReader = myCommand.ExecuteReader()
//逐一读取记录,显示到表格中
literalResult.Text = "<table border=1><tr><td>学号</td><td>姓名</td><td>性别</td>"
while (myReader.Read())
literalResult.Text = literalResult.Text + "<tr><td>" + myReader["StudentID"].ToString() + "</td><td>" + myReader[1].ToString() + "</td><td>" + myReader.GetString(2) + "</td>"
literalResult.Text = literalResult.Text + "</table>"
//关闭数据读取对象
myReader.Close()
//关闭数据库连接对象
myConnection.Close()
}
//响应"查询学生数量"按钮-演示"SqlCommand.ExecuteScalar()"方法
protected void btn2_Click(object sender, EventArgs e)
{
//创建数据库连接对象(SqlConnection)
SqlConnection myConnection = new SqlConnection("server=(local)database=schooluid=sapwd=123")
//创建数据库命令对象(SqlCommand)
SqlCommand myCommand = new SqlCommand("select count(*) from student",myConnection)
//打开数据库连接
myConnection.Open()
//获取查询结果数据(注意: SqlCommand.ExecuteScalar()方法只获取返回结果中的第一行、第一列值)
int StudentCount = Convert.ToInt32(myCommand.ExecuteScalar())
literalResult.Text = "学生数量为" + StudentCount.ToString()
//关闭数据库连接对象
myConnection.Close()
}
//响应"删除第一个学生"按钮-演示"SqlCommand.ExecuteNonQuery()"方法
protected void btn3_Click(object sender, EventArgs e)
{
//获取数据库连接字符串(ConfigurationManager类需要引入System.Configuration的命名空间)
string sqlConnectStr = "server=(local)database=schooluid=sapwd=123"
//创建数据库连接对象(SqlConnection)
SqlConnection myConnection = new SqlConnection(sqlConnectStr)
//创建数据库命令对象(SqlCommand)
SqlCommand myCommand = new SqlCommand("delete from student where StudentID=(select min(StudentID) from student)", myConnection)
//打开数据库连接
myConnection.Open()
//获取查询结果数据(注意: SqlCommand.ExecuteNonQuery()方法适合于Insert、Delete、Update等 *** 作,返回 *** 作影响的行数)
int Result = Convert.ToInt32(myCommand.ExecuteNonQuery())
if(Result==1)
literalResult.Text = "删除了1个学生记录!"
else
literalResult.Text = "删除 *** 作失败!"
//关闭数据库连接对象
myConnection.Close()
}
//响应"查询1号学生"按钮-演示如何调用"存储过程"
protected void btn4_Click(object sender, EventArgs e)
{
//创建数据库连接对象(SqlConnection)
SqlConnection myConnection = new SqlConnection("server=(local)database=schooluid=sapwd=123")
//创建数据库命令对象(存储过程类型)
SqlCommand myCommand = new SqlCommand("SearchStudentByID", myConnection)
myCommand.CommandType = System.Data.CommandType.StoredProcedure
//为存储过程添加参数(定义:Create Procedure SearchStudentByID @StudentID [int] AS Select * From Student Where StudentID=@StudentID)
myCommand.Parameters.AddWithValue("@StudentID", 1)
//打开数据库连接
myConnection.Open()
//创建数据读取对象(注意: ExecuteReader()方法获取的数据只读,而且只能向后移动记录)
SqlDataReader myReader = myCommand.ExecuteReader()
//逐一读取记录,显示到表格中
literalResult.Text = "<table border=1><tr><td>学号</td><td>姓名</td><td>性别</td>"
while (myReader.Read())
literalResult.Text = literalResult.Text + "<tr><td>" + myReader["StudentID"].ToString() + "</td><td>" + myReader[1].ToString() + "</td><td>" + myReader.GetString(2) + "</td>"
literalResult.Text = literalResult.Text + "</table>"
//关闭数据读取对象
myReader.Close()
//关闭数据库连接对象
myConnection.Close()
}
//响应"查询学生表"按钮-演示利用myDataSet类遍历表记录
protected void btn5_Click(object sender, EventArgs e)
{
//获取数据库连接字符串(ConfigurationManager类需要引入System.Configuration的命名空间)
string sqlConnectStr = ConfigurationManager.ConnectionStrings["SchoolConnectionString"].ConnectionString
//创建数据库连接对象(SqlConnection)
SqlConnection myConnection = new SqlConnection(sqlConnectStr)
//创建数据库命令对象(SqlCommand)
SqlCommand myCommand = new SqlCommand("select * from student", myConnection)
//打开数据库连接
myConnection.Open()
//创建DataAdapter对象
SqlDataAdapter myDataAdapter = new SqlDataAdapter(myCommand)
//创建DataSet对象,DataSetName="student"
DataSet myDataSet = new DataSet("student")
//把"student"表记录填充到myDataSet中
myDataAdapter.Fill(myDataSet, "student")
//遍历表记录,显示到表格中
literalResult.Text = "<table border=1><tr><td>学号</td><td>姓名</td><td>性别</td>"
int rowCount = myDataSet.Tables["student"].Rows.Count
for(int i=0i<=rowCount-1i++)
literalResult.Text = literalResult.Text + "<tr><td>" + myDataSet.Tables["student"].Rows[i]["StudentID"].ToString() + "</td><td>" + myDataSet.Tables["student"].Rows[i]["Name"].ToString() + "</td><td>" + myDataSet.Tables["student"].Rows[i]["Sex"].ToString() + "</td>"
literalResult.Text = literalResult.Text + "</table>"
//另外一种遍历表记录的方法
//literalResult.Text = "<table border=1><tr><td>学号</td><td>姓名</td><td>性别</td>"
//foreach (DataRow dr in myDataSet.Tables["student"].Rows)
//literalResult.Text = literalResult.Text + "<tr><td>" + dr["StudentID"].ToString() + "</td><td>" + dr["Name"].ToString() + "</td><td>" + dr["Sex"].ToString() + "</td>"
//literalResult.Text = literalResult.Text + "</table>"
//关闭数据库连接对象
myConnection.Close()
}
}
提交结果?是不是第一个页面的textbox里面的值啊什么的。在第一个页面.cs文件中把这些值获得,然后可跟在url后面,用session保存,还可以用hidden域传过去。再第二个页面用Response.Write(显示出来不就可以了吗
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)