主要是为了个人记忆,总结几个知识点: sqlconnection sqlcommand sqldataReader List<string>
主要是为了个人记忆,总结几个知识点: sqlconnection sqlcommand sqldataReader List<string>
下面是使用这几点的代码:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class DataReader : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SelectingData aa = new SelectingData();
List<string> ll=aa.GetTestaData();
foreach(string p in ll)
{
Response.Write(p.ToString()+"<br/>");
}
}
public class SelectingData
{
public List<string> GetTestaData()
{
SqlConnection conn;
SqlCommand cmd;
string cmdstr = "select a from t1";
conn = new SqlConnection("Server=localhost;uid=sa;pwd=1q2w3e!@#;database=test");
cmd = new SqlCommand(cmdstr,conn);
conn.Open();
SqlDataReader myReader;
List<string> returnData = new List<string>();
myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
while (myReader.Read())
{
returnData.Add(myReader["a"].ToString());
}
return returnData;
}
}
}