我正在尝试整理一个系统,其中从2个表格自动生成一个字母。一个表格包含该字母的所有内容,而另一个表格列出了要接收特定字母的人员。可能有多个人收到相同的字母,所以我正在尝试连接并循环生成每个字母。
我的问题是我正在遇到从数组读取数据库中的第二个条目时返回的第一个值。 IE我应该输出( Rob,Jess,Alex ),但我得到了( Jess,Jess,Alex )。
我不确定我的问题源自何处。
SQL = "SELECT * FROM S.dbo.letters l INNER JOIN
S.dbo.ds d ON l.sID= d.sID
WHERE l.id=@primaryID";
SqlConnection dbconn = new SqlConnection(connStr);
SqlCommand cmd = new SqlCommand(SQL, dbconn);
dbconn.Open();
cmd.Parameters.AddWithValue("@primaryID", 1);//Session["primIDedit"]
SqlDataReader myReader = cmd.ExecuteReader();
ArrayList letterInfo = new ArrayList();
ArrayList dFname = new ArrayList();
myReader.Read();
while (myReader.Read())
{
dFname.Add(myReader["dfname"]);
foreach (string i in dFname)
{
Label txtLabel1 = new Label(); //Generating a label
txtLabel1.Text = i.ToString();
ph1.Controls.Add(txtLabel1); //writing to a placeholder on the aspx page
//for now I'm just wanting to return all values.
}
sName.Text = myReader[1].ToString()
+ " " + myReader[3].ToString() + " " +
myReader[2].ToString();
paragraph1.Text = myReader[9].ToString();
paragraph2.Text = myReader[10].ToString();
paragraph3.Text = myReader[11].ToString();
signoff.Text = myReader[12].ToString();
}
答案 0 :(得分:1)
在while循环之前删除此行。
myReader.Read();
并在调试时使用断点检查myreader对象的下标值
sName.Text = myReader[1].ToString()
+ " " + myReader[3].ToString() + " " +
myReader[2].ToString();
paragraph1.Text = myReader[9].ToString();
paragraph2.Text = myReader[10].ToString();
paragraph3.Text = myReader[11].ToString();
signoff.Text = myReader[12].ToString();
答案 1 :(得分:1)
我在这里做了一些假设,但我会先创建两个类......
public class LetterInfo
{
public List<Recipient> Recipients {get; set;}
public string Paragraph1 {get; set;}
public string Paragraph2 {get; set;}
public string Paragraph3 {get; set;}
etc
public LetterInfo(){
Recipients = new List<Recipient>();
}
}
public class Recipient
{
public string FirstName {get; set;}
public string LastName {get; set;}
public string MiddleName {get; set;}
}
然后,改变你的db代码看起来像这样......
using (Sqldbconn dbconn = new Sqldbconn(connStr))
{
string sql = "SELECT l.Field, d.field FROM S.dbo.letters l
INNER JOIN S.dbo.ds d ON l.sID= d.sID WHERE l.id=@primaryID";
SqlCommand cmd = new SqlCommand(sql.ToString(), dbconn);
cmd.Parameters.AddWithValue("@primaryID", 1);//Session["primIDedit"]
dbconn.Open();
LetterInfo letterInfo = new LetterInfo();
var counter = 0;
using (var rdr = command.ExecuteReader())
{
while (rdr.Read())
{
var recipient = new Recipient();
recipient.FirstName = myReader["dfname"].ToString()
recipient.LastName = myReader["..."].ToString()
....
letterInfo.Recipients.Add(recipient);
//only need to populate the letterinfo once.
if (counter == 0){
//use field name here, not the ordinal number
letterInfo.Paragraph1 = myReader[9].ToString();
letterInfo.Paragraph2 = myReader[10].ToString();
letterInfo.Paragraph3 = myReader[11].ToString();
....
}
counter++;
}
}
}
然后,您可以使用letterInfo
对象填充表单。
您也可以将其移动到某种数据助手类中,并从您的代码隐藏中调用...
public LetterInfo GetLetterInfo(int primaryId){
//the code above in here
}
您的代码可能看起来像......
LetterInfo letter = dbHelper.GetLetterInfo(1);
p.s。我在Notepad ++中写了这个,所以我希望编译错误:)