将文本框值插入数据库,然后将其检索到列表框 [英] Insert the textbox value to database and retrive it to List Box

查看:103
本文介绍了将文本框值插入数据库,然后将其检索到列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨evryone ..
我想将文本框值添加到列表框.
我也希望此文本框值输入到称为Area的dabasex表中,并且@ esame time将此值插入到列表框.这是我使用的代码.



hi evryone..
i want to get the text box value to list box .
And also i want this textbox value enter into the dabasex table called Area and @ th esame time insert this value to List box. here is the code wich i have use.



SqlConnection conn = new SqlConnection(ConnectionString);
                String S = "Insert into Area values (''" + textBox1.Text.ToString().Trim() + "'')";
                SqlCommand command = new SqlCommand(S);
                SqlDataAdapter da = new SqlDataAdapter(S,ConnectionString);
              // DataSet dt = new DataSet();
                DataTable dt = new DataTable();
                da.Fill(dt);
                  
               textBox1.Clear();
                listBox1.Items.Add(dt.Rows[0].ItemArray[0].ToString());



但是这里的DataTable没有填充. any1可以告诉我这里是什么问题.
谢谢大家



but here DataTable is not filling. Can any1 tell me what is the problem here.
thank you everyone

推荐答案

问题出在这里;
The problems here are;

  1. INSERT语句不返回数据
  2. 直接从文本框中获取值将使您的代码遭受注入攻击,了解参数以及如何使用它们.
  3. 您不会为此不需要SqlDataAdapter或DataTable

  1. INSERT statements do not return data
  2. Taking a value direct from a text box opens your code to injection attack, learn about parameters and how to use them.
  3. You don''t need a SqlDataAdapter or DataTable for this
SqlCommand command = new SqlCommand(sqlQuery, conn);
conn.Open();
command.ExecuteNonQuery();
conn.Close();

//After Inserting value into database add the value to the list box, THEN clear the text box
listBox1.Items.Add(textBox1.Text.Trim());
textBox1.Clear();


这篇关于将文本框值插入数据库,然后将其检索到列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆