为什么combobox.text获取不了值

lkwu520 2013-04-17 08:32:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication8
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
string str = "Data Source=192.168.1.1;Database=cbyy;Uid=his;Pwd=samanagger";
SqlConnection conn = new SqlConnection(str);
string Sql = "select mc from zd_yp order by mc";
DataSet Ds = new DataSet();
SqlDataAdapter Da = new SqlDataAdapter(Sql, conn);
Da.Fill(Ds, "zd_ry");
comboBox1.DataSource = Ds.Tables["zd_ry"];
comboBox1.DisplayMember = "mc";
comboBox1.ValueMember = "mc";
comboBox1.SelectedValue = "mc";
}

public void button1_Click(object sender, EventArgs e)
{
string d1 = this.textBox1.Text + "-" + this.textBox2.Text + "-" + this.textBox3.Text;
string d2 = this.textBox4.Text + "-" + this.textBox5.Text + "-" + this.textBox6.Text;
string sql = mysql(d1, d2);
string str = "Data Source=192.168.1.1;Database=cbyy;Uid=his;Pwd=samanagger";
SqlConnection conn = new SqlConnection(str);
DataSet Ds = new DataSet();
SqlDataAdapter Da = new SqlDataAdapter(sql, conn);
Da.Fill(Ds, " ");
dataGridView1.DataSource = Ds.Tables[" "];
textBox7.Text = sql;

}





public void textBox1_TextChanged(object sender, EventArgs e)
{
int i;
if (textBox1.TextLength == 4)
{
this.textBox2.Focus();
}
if (int.TryParse(textBox1.Text, out i) == false)
{
MessageBox.Show("请输入正确的日期");
textBox1.Text = "1";
textBox1.Focus();
}

}

public void textBox2_TextChanged(object sender, EventArgs e)
{
if (textBox2.TextLength == 2)
{
this.textBox3.Focus();
}
int i;
if (int.TryParse(textBox2.Text, out i) == false || Convert.ToInt32(textBox2.Text) > 12)
{
MessageBox.Show("请输入正确的日期");
textBox2.Text = "1";
textBox2.Focus();
}

}

public void textBox3_TextChanged(object sender, EventArgs e)
{
if (textBox3.TextLength == 2)
{
this.textBox4.Focus();
}
int i;
if (int.TryParse(textBox3.Text, out i) == false || Convert.ToInt32(textBox3.Text) > 31)
{
MessageBox.Show("请输入正确的日期");
textBox3.Text = "1";
textBox3.Focus();
}


}

public void textBox4_TextChanged(object sender, EventArgs e)
{
if (textBox4.TextLength == 4)
{
this.textBox5.Focus();
}
int i;
if (int.TryParse(textBox4.Text, out i) == false)
{
MessageBox.Show("请输入正确的日期");
textBox4.Text = "1";
textBox4.Focus();
}


}

public void textBox5_TextChanged(object sender, EventArgs e)
{
if (textBox5.TextLength == 2)
{
this.textBox6.Focus();
}
int i;
if (int.TryParse(textBox5.Text, out i) == false || Convert.ToInt32(textBox5.Text) > 12)
{
MessageBox.Show("请输入正确的日期");
textBox5.Text = "1";
textBox5.Focus();
}



}

public void textBox6_TextChanged(object sender, EventArgs e)
{
int i;
if (int.TryParse(textBox6.Text, out i) == false || Convert.ToInt32(textBox6.Text) > 31)
{
MessageBox.Show("请输入正确的日期");
textBox6.Text = "1";
textBox6.Focus();
}
if (int.TryParse(textBox6.Text, out i) == true || Convert.ToInt32(textBox6.Text) < 10)
{
this.textBox6.Text = textBox6.Text.PadLeft(2, '0');
}

}


static string days(DateTime date)
{
DateTime starttime = Convert.ToDateTime("1899-12-30");
DateTime endtime = date;
TimeSpan day = endtime - starttime;
string days = Convert.ToString(day.Days);
return days;
}

public static string mysql(string date1, string date2)
{
DateTime starttime = Convert.ToDateTime(date1);
DateTime endtime = Convert.ToDateTime(date2);
Form1 f = new Form1();
string sql = "select * from zy_bhjfyz" + Convert.ToString(starttime.Year) + starttime.Month.ToString("D2") + " where flag & 16=0 and flag & 2>0 and rq between " + days(Convert.ToDateTime(date1)) + " and " + days(Convert.ToDateTime(date2)) + " and yzmc like '%" +f.comboBox1.Text+"%'";
int y, m, i;
m = endtime.Month - starttime.Month;
y = endtime.Year - starttime.Year;
if (y == 0)
{
for (i = starttime.Month; i <endtime.Month; i++)
{
sql = sql + " union all " + "select * from zy_bhjfyz" + Convert.ToString(starttime.Year) + starttime.AddMonths(i).Month.ToString("D2") + " where flag & 16=0 and flag & 2>0 and rq between " + days(Convert.ToDateTime(date1)) + " and " + days(Convert.ToDateTime(date2)) + " and yzmc like '%" + f.comboBox1.Text + "%'";
}
}
else
{
for (i = starttime.Month + 1; i <= 12; i++)
{
sql = sql + " union all " + "select * from zy_bhjfyz" + Convert.ToString(starttime.Year) + i.ToString("D2") + " where flag & 16=0 and flag & 2>0 and rq between " + days(Convert.ToDateTime(date1)) + " and " + days(Convert.ToDateTime(date2)) + " and yzmc like '%" + f.comboBox1.Text + "%'";
}
for (i = starttime.Year + 1; i < endtime.Year; i++)
{
int s;
for (s = 1; s <= 12; s++)
{
sql = sql + " union all " + "select * from zy_bhjfyz" + Convert.ToString(i) + s.ToString("D2") + " where flag & 16=0 and flag & 2>0 and rq between " + days(Convert.ToDateTime(date1)) + " and " + days(Convert.ToDateTime(date2)) + " and yzmc like '%" + f.comboBox1.Text + "%'";
}
}

for (i = 1; i < endtime.Month; i++)
{
sql = sql + " union all " + "select * from zy_bhjfyz" + Convert.ToString(endtime.Year) + i.ToString("D2") + " where flag & 16=0 and flag & 2>0 and rq between " + days(Convert.ToDateTime(date1)) + "and " + days(Convert.ToDateTime(date2)) + " and yzmc like '%" + f.comboBox1.Text + "%'";
}


}
return sql;
}



}





}



函数 mysql里面的SQL获取不了combobox.text的值,这个值总是为空,请问下是怎么回事,头都炸了。。
...全文
251 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
狼烟辉 2013-04-17
  • 打赏
  • 举报
回复
Form1 f = new Form1(); 表示新初始化了一个Form1,他的comboBox1自然没有值。
lkwu520 2013-04-17
  • 打赏
  • 举报
回复
找到问题了,谢啦~
  • 打赏
  • 举报
回复
Form1 f = new Form1(); ... f.comboBox1.Text 为什么要new一个Form1,难道不是用this.comboBox1.Text么?

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧