StudentID > Number
StudentName > Text
StudentCNIC > Text
StudentDOB > Date/Time
我试图创建一个搜索文本框,其中的结果显示在文本框中。我有一个叫做FindBtn的按钮,其中用户输入StudentID,学生姓名或学生CNCI(只是疼痛数字)。结果将显示在文本框... StudIDTb(显示学生ID),StudNameTb(显示学生姓名),StudCNCITb(显示学生CNCI)和StudDOBTb(显示学生DOB)中。数据库访问搜索文本框
我在互联网上看到的很多例子使用gridview,但我使用文本框来显示结果。我已经找到感兴趣的那些是...
public Form1()
{
InitializeComponent();
//Connection String for Access 2003.
myCon = new OleDbConnection(@" Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C....\StudDB.mdb ");
}
private void Form1_Load(object sender, EventArgs e)
{
this.studentsTableAdapter.Fill(this.studDBDataSet.Students);
}
在FindBtn功能做了以下....
private void FindBtn_Click(object sender, EventArgs e)
{
string title = textBox1.Text.ToString();
if (title != "")
{
string queryString = "SELECT * FROM Students + title;
//I need help here
StudIDTb.Text = queryString;
StudNameTb.Text = queryString;
StudCNCITb.Text = queryString;
StudDOBTb.Text = queryString;
}
else
MessageBox.Show("Please try again\nError: ");
}
结果表明。 ...
SELECT *
FROM Students 103
在每个文本框和EXE冻结,所以显然这是错误的。请有人帮助我在这里,提前谢谢。
UPDATE 1 我已经包括的OleDbCommand,的CommandType和只是字符串变量之前开口连接,其防止屏幕的冻结,但它并没有解决问题
UPDATE 2 找到东西我喜欢,但它不工作 Here
更新3
作为C语言尖角,友情提供的一段代码....
private void btnFind_Click(object sender, EventArgs e)
{
string title = textBox1.Text.ToString();
string queryString = "SELECT * FROM Students" + title;
OleDbCommand command = new OleDbCommand();
command.CommandText = queryString;
command.Connection = myCon;
myCon.Open();
OleDbDataReader dr = command.ExecuteReader();
while (dr.Read())
{
StudIDTb.Text += String.Format("Student ID:
{0}\n",dr["StudID"].ToString());
StudNameTb.Text += String.Format("StudentName ID: {0}\n",
dr["StudentName"].ToString());
StudCNCITb.Text += String.Format("StudentCNIC: {0}\n",
dr["StudentCNIC"].ToString());
StudDOBTb.Text += String.Format("StudentCNIC: {0}\n",
dr["StudentCNIC"].ToString());
}
myCon.Close();
我修改格式化之后接收到错误....
“Microsoft Jet数据库引擎无法找到输入表或查询 'Students101'。请确保它存在并且其名称拼写是否正确”。
我在看它现在要找出它意味着
这个错误是因为你的查询是错误的,你应该包括在查询一些你的参数: 的queryString =“SELECT * FROM学生,其中StudentName =” + txtStudentNameTb.Text +“或StudentCNIC =”+ txtStudentCNIC.Text 但这不是“安全”的方式,我对querys等知道不多,但是你应该google关于参数化querys的一些东西,应该有足够的解释 – 2013-03-09 20:04:26