从文本框填充数据库(SQL) [英] fill database from textbox(SQL)

查看:89
本文介绍了从文本框填充数据库(SQL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我想从textbox填充一个包含1个表(1行:name)的数据库。当用户在文本框中输入他的名字时(在表单上),他的名字应该添加到数据库中。怎么??

hello, i want to fill a database having 1 table (1 row:name) from textbox.when the user enter his name on the textbox(on the form) his name should add in database. how??

推荐答案

尝试:
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("INSERT INTO myTable ([Name]) VALUES (@NAME)", con))
        {
        cmd.Parameters.AddWithValue("@NAME", textBoxWithTheNameIn.Text);
        cmd.ExecuteNonQuery();
        }
    }

但是......一旦你得到第二个名为Brian或John Smith的访客,它就会失败。

您需要考虑添加ID列 - 您可以将其设置为IDENTITY,SQL将确保它始终不同。

But...it's going to fail as soon as you get a second visitor called "Brian" or "John Smith".
You need to consider adding an ID column - you can make it IDENTITY and SQL will take care of ensuring it's always different.


这篇关于从文本框填充数据库(SQL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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