在开放连接中使用数组 [英] use of array in a open connection

查看:75
本文介绍了在开放连接中使用数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我可以使用
这样的数组吗

Hi,
Can i use an array like

public partial class BILL3 : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["order"].ConnectionString);
    Decimal a;
    DataTable dt;
    int i = 0;
    Decimal [] b=new b[10];
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = Session["name"].ToString();
        Label2.Text = DateTime.Now.ToShortDateString() ;
        Label3.Text = Session["sdate"].ToString();
        Label4.Text = Session["edate"].ToString();
        con.Open();
        DateTime dt1 = Convert.ToDateTime(Label3.Text);
        DateTime dt2 = Convert.ToDateTime(Label4.Text);
    
      
        
         SqlDataAdapter da=new SqlDataAdapter("select SNO,DATE,PNAME,QUANT,RATE,TOTAL from BILL1 where CNAME='"+Label1.Text+"' and DATE between '" + dt1.ToShortDateString() + "' and '" + dt2.ToShortDateString() + "'",con);
        DataSet ds=new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        con.Close();
    
        /////////////
        con.Open();
        SqlCommand cmd = new SqlCommand("select SNO,DATE,PNAME,QUANT,RATE,TOTAL from BILL1 where CNAME='" + Label1.Text + "' and DATE between '" + dt1.ToShortDateString() + "' and '" + dt2.ToShortDateString() + "'", con);
    
        SqlDataReader dr1 = cmd.ExecuteReader();
        while (dr1.Read())
        {
    
            
            a += Convert.ToDecimal(dr1["TOTAL"].ToString());
            b[i] = a;
            i++;
         
            
        }
        Label5.Text = a.ToString();
        Label6.Text = a.ToString();
        con.Close();
        foreach(int i in b)
        {
            Console.WriteLine(b);
        }    
    }
}

推荐答案


您的声明应为:
Hi,
Your declaration should be:
Decimal a;
DataTable dt;
int i = 0;
decimal [] b = null;


循环应为:


Looping should be:

b = new decimal[dr1.FieldCount];
while (dr1.Read())
{
    a += Convert.ToDecimal(dr1["TOTAL"].ToString());
    b[i] = a;
    i++;
}


并且:


And:

foreach(decimal i in b)
{
    //Console.WriteLine(b);
    // Why Console.WriteLine here? For Web Application you can use Reponse.Write()
    // And why you are writing array directly? No need. Write its content.
    Reponse.Write(i.ToString());
}


最后:


Finally:

for (int x = 0; x <= b.Length - 1; x++)
{
    TableCell tc = new TableCell();
    tc.Text = b[x].ToString();
    GridView1.Rows[x].Cells.Add(tc);
}



--Amit



--Amit


这篇关于在开放连接中使用数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆