代码如下:
public string[] shuju =new string[9999];
public string Getread(string cha, string search)
{
string shuchu="";
id from reg where "+cha+"='" + search + "'";
SqlDataReader thisreader = thiscommand.ExecuteReader();
while (thisreader.Read())
{
shuchu+=thisreader["id"];
}
thisreader.Close();
return shuchu;
}
private void button1_Click(object sender, EventArgs e)
{
string output="";
if ((this.shuru.Text.Length == 0) && (shuju[0] == null))
{
MessageBox.Show("请输入要查询的信息或者读取文件中的信息");
return;
}
else
{
SqlConnection thisconnection = new SqlConnection(qu());
try
{
thisconnection.Open();
}
catch (SqlException)
{
MessageBox.Show("数据库连接失败,请联系管理员");
Application.Exit();
}
thiscommand = thisconnection.CreateCommand();
if (shuru.Text.Length != 0)
{
output += Getread(cha(), this.shuru.Text) + "\r\n";
}
else
{
for (int x=0;x<daxiao;x++)
{
output += Getread(cha(), shuju[x]) + "\r\n";
}
}
thisconnection.Close();
this.shuru.Text = null;
this.textBox1.Text = output;
}
}
private void duwenjian()
{
string chaxun;
try
{
FileStream wenjian = new FileStream("chaxun.txt", FileMode.Open);
StreamReader cha = new StreamReader(wenjian,System.Text.Encoding.GetEncoding("gb2312"));
chaxun = cha.ReadLine();
while (chaxun != null)
{
for (int x = 0; x <= shuju.GetUpperBound(0); x++)
{
chaxun = cha.ReadLine();
shuju[x] = chaxun;
daxiao++;
}
}
MessageBox.Show("文件读取成功!");
cha.Close();
}
catch (IOException e)
{
MessageBox.Show(e.ToString());
}
catch (SystemException)
{
MessageBox.Show("文件读取失败,请确认chaxun.txt在当前目录中");
}
}