【问题标题】:How to read data in each row and each column from sql table in asp.net using C#?如何使用C#从asp.net中的sql表中读取每一行和每一列的数据?
【发布时间】:2014-02-20 19:29:49
【问题描述】:

Sql 数据库是StudentInfo,表名是Registration

ID----------Name---------------Email---------------------------PhoneNo
1           Munasunghe        amilamunasinghe@yahoo.com        0717069425    
2           Liyanarachchi     hareshliya6@gmail.com            0756706352   

protected void Page_Load(object sender, EventArgs e)
{
    string query = "select ID, Name, Email, PhoneNo from Registration"; 
    SqlCommand cmd1 = new SqlCommand(query);
    DataTable dt1 = GetData(cmd1);
    int rowcount = dt1.Rows.Count;
    /* I want to read data in each row step by step and assign to variables*/

}

GetData函数用于从数据库中获取数据。

 private DataTable GetData(SqlCommand cmd)
    {
        DataTable dt = new DataTable();
        String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
        SqlConnection con = new SqlConnection(strConnString);
        SqlDataAdapter sda = new SqlDataAdapter();
        cmd.CommandType = CommandType.Text;
        cmd.Connection = con;
        try
        {
            con.Open();
            sda.SelectCommand = cmd;
            sda.Fill(dt);
            return dt;
        }
        catch
        {
            return null;
        }
        finally
        {
            con.Close();
            sda.Dispose();
            con.Dispose();
        }
    }

ID 是主键。

结果应该是这样的(姓名,电子邮件,电话号码是变量,1,2,...是ID值)

Name[1]=Munasunghe
Name[2]=Liyanarachchi
Email[1]=amilamunasinghe@yahoo.com  
Email[2]=hareshliya6@gmail.com
Phone No[1]=0717069425
Phone No[2]=0756706352

【问题讨论】:

标签: c# asp.net sql-server


【解决方案1】:

我会说你首先创建一个新的类来存储你的数据(比如 StudentInfo)

public class StudentInfo
{
    public StudentInfo(int ID, string Name, string Email, string PhoneNo)
    {
        this.ID = ID;
        this.Name = Name;
        this.Email = Email;
        this.PhoneNo = PhoneNo;
    }
    public int ID { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public string PhoneNo { get; set; }
}

然后使用这个函数返回一个 StudentInfo 类的列表

public List<StudentInfo> GetData()
{
    List<StudentInfo> data = new List<StudentInfo>();
    SqlConnection con = new SqlConnection("Your connection string");
    SqlCommand command = new SqlCommand("SELECT * FROM [Registration]", con);
    con.Open();
    SqlDataReader sdr = command.ExecuteReader();
    while(sdr.Read())
    {
         data.Add((int)sdr["ID"], (string)sdr["Name"], (string)sdr["Email"], (string)sdr["PhoneNo"]);
    }
    con.Close();
    return data;
}

然后你这样使用它:

List<StudentInfo> info = GetData();
foreach(StudentInfo si in info)
{
     Response.Write("<h3>ID is " + si.ID + "</h3><p>StudentName is " + si.Name + "</p>");
}

要更新值,请执行以下操作:

public void SetValue(int StudentID, String NewName, String NewEmail, String NewPhone)
{
    SqlConnection con = new SqlConnection("Your connection string");
    SqlCommand command = new SqlCommand("UPDATE [Registration] SET [Name]='" + NewName + "', [Email]='" + NewEmail + "', [PhoneNo]='" + NewPhone + "' WHERE [ID]=" + StudentID + "", con);
    con.Open();
    command.ExecuteNonQuery();
    con.close();
}

我建议你阅读一些关于 sql 的文章

【讨论】:

  • 我想使用标签检索 .aspx 文件中的数据。所以举个例子如何编写foreach循环。
  • 假设我想要电子邮件[2]=hareshliya6@gmail.com 和电话号码[1]=0717069425。那我怎么办?请帮帮我
  • @PradeepHerath 你想在运行时设置它吗?
  • @Ravshajon 我想使用 SqlDataReader sdr = command.ExecuteReader(); 获取特定的单元格数据怎么办?我不想更新。假设我想将 Email[2] 作为变量。
  • SqlCommand command = new SqlCommand("SELECT [Email] FROM [tablename] WHERE [ID]='Id you want'", con); con.Open();字符串电子邮件 = commandExecuteScalar(); con.close();
猜你喜欢
  • 1970-01-01
  • 2017-09-30
  • 1970-01-01
  • 2021-09-19
  • 1970-01-01
  • 2021-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多