我想实现这样的功能,从不同的数据表里取出不同的字段在D盘下生成不同的文件,想点击一下button1事件后都可以生成不同的文件,请问if else条件语句怎么写(怎么用StrSql做条件?),就是标红的地方不会写(程序生成没有问题)
private void button1_Click(object sender, EventArgs e)
{
if(条件语句)
{
SqlConnection SqlCon = new SqlConnection("Data Source=local;Database=dbshort;User id=qqq;PWD=xxx");
SqlCon.Open();
string StrSql = "select 时段,人物 from nowtable where 书名 = '短篇小说'";
SqlDataAdapter Sda = new SqlDataAdapter(StrSql, SqlCon);
DataSet Ds = new DataSet();
Sda.Fill(Ds);
SqlCon.Close();
FileInfo Fi = new FileInfo("D:/xx.txt");
StreamWriter Sw = Fi.CreateText();
for (int i = 0; i < Ds.Tables[0].Rows.Count; i++)
{
for (int j = 0; j < Ds.Tables[0].Columns.Count; j++)
{
if (j != 0)
{
Sw.Write(",");
}
Sw.Write(Ds.Tables[0].Rows[i][j].ToString());
}
Sw.WriteLine("");
}
Sw.Flush();
}
}
else(条件语句)
{
SqlConnection SqlCon = new SqlConnection("Data Source=local;Database=dblong;User id=qqq;PWD=xxx");
SqlCon.Open();
string StrSql = "select 地点,人物 from longtable where 书名 = '长篇小说'";
SqlDataAdapter Sda = new SqlDataAdapter(StrSql, SqlCon);
DataSet Ds = new DataSet();
Sda.Fill(Ds);
SqlCon.Close();
FileInfo Fi = new FileInfo("D:/aa.txt");
StreamWriter Sw = Fi.CreateText();
for (int i = 0; i < Ds.Tables[0].Rows.Count; i++)
{
for (int j = 0; j < Ds.Tables[0].Columns.Count; j++)
{
if (j != 0)
{
Sw.Write(",");
}
Sw.Write(Ds.Tables[0].Rows[i][j].ToString());
}
Sw.WriteLine("");
}
Sw.Flush();
}
}
private void button1_Click(object sender, EventArgs e)
{
if(条件语句)
{
SqlConnection SqlCon = new SqlConnection("Data Source=local;Database=dbshort;User id=qqq;PWD=xxx");
SqlCon.Open();
string StrSql = "select 时段,人物 from nowtable where 书名 = '短篇小说'";
SqlDataAdapter Sda = new SqlDataAdapter(StrSql, SqlCon);
DataSet Ds = new DataSet();
Sda.Fill(Ds);
SqlCon.Close();
FileInfo Fi = new FileInfo("D:/xx.txt");
StreamWriter Sw = Fi.CreateText();
for (int i = 0; i < Ds.Tables[0].Rows.Count; i++)
{
for (int j = 0; j < Ds.Tables[0].Columns.Count; j++)
{
if (j != 0)
{
Sw.Write(",");
}
Sw.Write(Ds.Tables[0].Rows[i][j].ToString());
}
Sw.WriteLine("");
}
Sw.Flush();
}
}
else(条件语句)
{
SqlConnection SqlCon = new SqlConnection("Data Source=local;Database=dblong;User id=qqq;PWD=xxx");
SqlCon.Open();
string StrSql = "select 地点,人物 from longtable where 书名 = '长篇小说'";
SqlDataAdapter Sda = new SqlDataAdapter(StrSql, SqlCon);
DataSet Ds = new DataSet();
Sda.Fill(Ds);
SqlCon.Close();
FileInfo Fi = new FileInfo("D:/aa.txt");
StreamWriter Sw = Fi.CreateText();
for (int i = 0; i < Ds.Tables[0].Rows.Count; i++)
{
for (int j = 0; j < Ds.Tables[0].Columns.Count; j++)
{
if (j != 0)
{
Sw.Write(",");
}
Sw.Write(Ds.Tables[0].Rows[i][j].ToString());
}
Sw.WriteLine("");
}
Sw.Flush();
}
}
比如一个rbtn或者一个ddl选择后value值就是长篇或短篇
这样btn按后就可以做条件判断了
无论如何,得给用户提供选择啊,比如6楼说的,在界面上放置 RadioButton 或者 DropDownList,然后在 button1_Click 里判断相应控件的 SelectedValue,从而控制最后的结果。