已经有一个与这个命令相关联的打开的数据读取器,必须先关闭它
本文关键字:读取 数据 有一个 命令 关联 | 更新日期: 2023-09-27 18:09:46
我正在制作一个[WebMethod] . .我想在里面添加一行....我想要信用卡和借方…?
public DataSet getleger(string accno, string fromdate, string todate)
{
SqlConnection con = new SqlConnection(@"Data Source=123-PC;Initial Catalog=bcounts;Persist Security Info=True;User ID=Saba;Password=123");
con.Open();
SqlCommand cmd = new SqlCommand("select Convert(nvarchar(10),gt.Value_Date,111) as [Value_Date],gt.Voucher_no+'-'+gr.VchrType as voucher,gt.Acct_Nirration,gr.InstrumentNo,gt.Dr_Amount,gt.Cr_Amount from gl_transaction gt, Gl_Ref gr where gt.Accountno = '" + accno + "' and gt.Voucher_No=gr.Voucher_no and gt.Value_Date between '" + fromdate + "' and '" + todate + "'", con);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataSet ds = new DataSet("Ledger");
adp.Fill(ds);
con.Close();
}
我不会这样写。内联变量是SQL注入的源。你真应该这样做。
我也会把你的连接字符串移出你的方法。不需要重复声明。
private SqlConnection con { get; set; }
public void SetConnection(string server, string database, string username, string password)
{
con = new SqlConnection(String.Format("Data Source={0};Initial Catalog={1};Persist Security Info=True;User ID={2};Password={3}", server, database, username, password));
}
public DataSet getleger(string accno, string fromdate, string todate)
{
try
{
con.Open();
using (SqlCommand cmd = con.CreateCommand())
{
cmd.CommandText = "select Convert(nvarchar(10),gt.Value_Date,111) as [Value_Date],gt.Voucher_no+'-'+gr.VchrType as voucher,gt.Acct_Nirration,gr.InstrumentNo,gt.Dr_Amount,gt.Cr_Amount from gl_transaction gt, Gl_Ref gr where gt.Accountno = @accno and gt.Voucher_No=gr.Voucher_no and gt.Value_Date between @fromdate and @todate";
cmd.Parameters.AddWithValue("@accno", accno);
cmd.Parameters.AddWithValue("@fromdate", fromdate);
cmd.Parameters.AddWithValue("@todate", todate);
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
using (DataSet ds = new DataSet())
{
da.Fill(ds);
return ds;
}
}
}
}
catch (SqlException se)
{
//handle error
}
}
向数据集添加一行
DataRow row = ds.Tables["TableName"].NewRow();
row["Column1"] = "Stuff";
row["Column2"] = "More Stuff";
ds.Tables["TableName"].Rows.Add(row);
如果你想从查询中总结列,你应该在sql查询中这样做。
SELECT SUM(column_name) FROM table_name;