在文本框中显示所选数据? [英] displayed selected data in textbox ?
问题描述
你好每一个,
i做了一个包含两个文本框的窗体,第一个有单词,第二个应该有该单词的含义。
i有一个按钮,当它被点击它将文本框2中的含义相对于textbox1中的单词,但我不执行??错误是:无法创建字段audio1的子列表。
我使用过sqlserver& visual studio 2010
这是我的代码:
private void btntranslate_Click_1( object sender,EventArgs e)
{
if (textBox1.Text == )
{
MessageBox.Show( 没有要翻译的单词!< /跨度>);
}
else
{
string constring = Data Source = .; Initial Catalog = speakerpro; Integrated Security = True;
string concom = 从[选择均值] audio1]其中word =' + textBox1.Text + ';;
SqlConnection con = new SqlConnection(constring);
SqlCommand com = new SqlCommand(concom,con);
SqlDataAdapter adp = new SqlDataAdapter();
DataTable dt = new DataTable();
con.Open();
adp.SelectCommand = com;
adp.Fill(dt);
textBox2.DataBindings.Add( Text,dt, audio1.mean);
SqlDataReader dr = com.ExecuteReader();
con.Close();
}
}
我的代码有什么问题???引导我,请
更正了格式问题。
已添加 pre
标签。
[/编辑]
问题,我可以看到
- 使用参数化查询而不是内联查询来避免SQLInjection攻击。更多内容 - 无法打开sql server连接 [ ^ ]
- 使用
SqlDataAdapter
时无需打开或关闭Connection。SqlDataAdapter
会自动为您做这些。- 您最后声明了
SqlDataReader
,这是根本不需要。
private DataTable getmeaning( text1)
{
DataTable dt = new DataTable();
dt.Columns.Add( mean, typeof ( string ));
if (conn.State.ToString()== 已关闭)
{
conn.Open();
}
SqlCommand newCmd = conn.CreateCommand();
newCmd.Connection = conn;
newCmd.CommandType = CommandType.Text;
newCmd.CommandText = 从[audio1]中选择均值,其中word =' + text1 + ';;
SqlDataReader dr = newCmd.ExecuteReader();
while (dr.Read())
{
dt.Rows.Add(dr [ 表示]);
}
conn.Close();
return dt;
}
然后调用这样的方法....
DataTable dt = getmeaning(textBox1.Text);
if (dt.Rows.Count > 0 )
{
DataView dv = new DataView(dt);
textBox2.Text = dv [ 0 ] [ 表示跨度>]的ToString();
}
尝试
textBox2.DataBindings.Add( Text,dt, 表示跨度>);
hello every one ,
i have done a windows form which contains two textboxes , first has the word , and the second should has the meaning of that word .
i have a button , when it is clicked it will but the meaning in textbox2 with respect to the word in textbox1 , but i does not perform ?? the error is : Child list for field audio1 cannot be created.
i have used sqlserver & visual studio 2010
this is my code :
private void btntranslate_Click_1(object sender, EventArgs e)
{
if (textBox1.Text == " ")
{
MessageBox.Show(" There is no word to translated! ");
}
else
{
string constring = "Data Source=.;Initial Catalog=speakerpro;Integrated Security=True";
string concom= "select mean from [audio1] where word = ' " + textBox1.Text + " ' ;";
SqlConnection con = new SqlConnection(constring);
SqlCommand com = new SqlCommand(concom , con);
SqlDataAdapter adp = new SqlDataAdapter();
DataTable dt = new DataTable();
con.Open();
adp.SelectCommand = com;
adp.Fill(dt);
textBox2.DataBindings.Add("Text", dt , "audio1.mean");
SqlDataReader dr = com.ExecuteReader();
con.Close();
}
}
is there any problem in my code ??? guide me ,please
[Edit member="Tadit"]
Corrected formatting issues.
Added pre
tags.
[/Edit]
Problems, which I can see
- Use paramterized Query instead of inline Queries to avoid SQLInjection attack. More at - could not open sql server connection[^]
- No need to open or close the Connection while using
SqlDataAdapter
.SqlDataAdapter
does these automatically for you.- You have declared a
SqlDataReader
at last, which is not needed at all.
private DataTable getmeaning(text1) { DataTable dt = new DataTable(); dt.Columns.Add("mean", typeof(string)); if (conn.State.ToString() == "Closed") { conn.Open(); } SqlCommand newCmd = conn.CreateCommand(); newCmd.Connection = conn; newCmd.CommandType = CommandType.Text; newCmd.CommandText = "select mean from [audio1] where word = ' " + text1 + " ' ;"; SqlDataReader dr = newCmd.ExecuteReader(); while (dr.Read()) { dt.Rows.Add(dr["mean"]); } conn.Close(); return dt; }
then call the method like this....
DataTable dt = getmeaning(textBox1.Text); if(dt.Rows.Count > 0) { DataView dv = new DataView(dt); textBox2.Text = dv[0]["mean"].ToString(); }
Try textBox2.DataBindings.Add("Text",dt,"mean");
这篇关于在文本框中显示所选数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!