如何将两行SQL表显示为两个不同的文本框?
大家好,
我有两个名为输出和活动的表。 Output表包含作为主键的Output_ID(Int)和Output(Varchar(Max)),Activities表包含作为主键的Activity_ID(Int),作为外键的Output_ID(Int)和它自己的Activity(Varchar)。我在这些表之间使用了一对多的关系,其中一个输出可以有多个活动,但一个活动只能有一个输出。
我想要你的帮助是如何根据Output_ID检索输出的不同文本框中的所有活动?到目前为止,我对SQL Server很陌生,如果你帮助我,我会很高兴。
注意:我试过这段代码,它适用于SQL Server查询但不适用于visual studio c#。
Hi All,
I have two Tables Named Output and Activities. The Output table contains Output_ID (Int) as a primary key and Output (Varchar(Max)), The Activities table contains Activity_ID (Int) as a primary key, Output_ID (Int) as foreign key and Activity (Varchar) it self. I used one to many relation ship between these tables where an output can have multiple activities but an activity can only have only one output.
The thing i want your help with is How can i retrieve the all activities in different textboxes of an output based on Output_ID? as far i am quite new to SQL Server i will be glad if you help me.
Note : I tried this code, it works with SQL Server Query but not with visual studio c#.
SELECT * FROM ActivitesTBL
提前付款
这就是我所做的。
Thanks in advance
Here is what i have done.
SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Joining ;Persist Security Info=True;User ID=sa;Password=*********");
string Query = "SELECT * FROM ActivityTBL WHERE Output_ID = '"+Outputcmbx.Text+"';".ToString();
SqlCommand cmd = new SqlCommand(Query, conn);
SqlDataReader dr;
try
{
conn.Open();
dr = cmd.ExecuteReader();
while (dr.Read())
{
string sActivity = dr.GetString(2);
FirstActivitytxt.Text = sActivity;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
您好Naser,
来自您的更新的问题,你只检索了一个字符串并移动到FirstActivityTxt.text,
你错过了读第二个字符串...所以试试这个
Hi Naser,
From your updated question, you retrieved only one string and moved to FirstActivityTxt.text,
you missed to read second string... so try this
SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Joining ;Persist Security Info=True;User ID=sa;Password=*********");
string Query = "SELECT * FROM ActivityTBL WHERE Output_ID = '"+Outputcmbx.Text+"';".ToString();
SqlCommand cmd = new SqlCommand(Query, conn);
SqlDataReader dr;
try
{
conn.Open();
dr = cmd.ExecuteReader();
while (dr.Read())
{
string sActivity = dr.GetString(2);
string poorActivity = dr.GetString(1); //Column ordinal number comes here
FirstActivitytxt.Text = sActivity;
PoorActivitytxt.Text = poorActivity; //Replace PoorActivitytxt name with your textbox name
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
我想现在它会起作用....
问候,
RK
I think now it would work....
Regards,
RK