如何使用C#语言中插入记录的数据库? [英] How to insert Records in Database using C# language?

查看:140
本文介绍了如何使用C#语言中插入记录的数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是一个初学者对C#,所以我需要太多的帮助。现在的问题是,我已经设计了一个窗口形式,其中有很多字段,如姓,名字,地址等。现在我想要做的是,当我填写表格,然后单击插入按钮的所有信息进入数据库。有谁知道该怎么做?



 私人无效的button1_Click(对象发件人,发送System.EventArgs)
$ { b $ b字符串connetionString = NULL;
SqlConnection的CNN;
SqlDataAdapter的适配器=新的SqlDataAdapter();
字符串SQL = NULL;
connetionString =数据源= UMAIR;初始目录=空气; Trusted_Connection = TRUE; ;

CNN =新的SqlConnection(connetionString);
SQL =插入到主(Firt名字,姓氏)值(textbox2.Text,textbox3.Text);


{
cnn.Open();
adapter.InsertCommand =新的SqlCommand(SQL,美国有线电视新闻网)
adapter.InsertCommand.ExecuteNonQuery();
MessageBox.Show(插入行!!);
}
赶上(异常前)
{
MessageBox.Show(ex.ToString());
}
}


解决方案

有在查询大量的问题。结果
这是你的代码的修改版本

 字符串connetionString = NULL ; 
字符串SQL = NULL;
connetionString =数据源= UMAIR;初始目录=空气; Trusted_Connection = TRUE; ;
使用(SqlConnection的CNN =新的SqlConnection(connetionString))
{
SQL =插入到主([Firt名],[姓氏])值(@第一,@最后的) ;
cnn.Open();使用
(CMD的SqlCommand =新的SqlCommand(SQL,美国有线电视新闻网))
{
cmd.Parameters.AddWithValue(@第一,textbox2.text);
cmd.Parameters.AddWithValue(@最后的,textbox3.text);
cmd.ExecuteNonQuery();
MessageBox.Show(插入行!!);
}
}




  • 的列名包含(这应该是可以避免的),所以你
    空间需要他们

  • 方括号括
  • 您需要使用使用语句要确保连接
    将被关闭,资源发布

  • 您将直接控制字符串中,但是这不工作

  • 您需要使用参数化查询,以避免报价的问题和
    sqlinjiection攻击

  • 无需使用DataAdapter一个简单的插入查询



除此之外,还有其他的潜在的问题。如果用户确实在文本框控件没有输入任何东西?你已经对此做出检查试图插入过吗?
正如我刚才所说的字段名称包含空格,这将导致的不便你的代码。试着改变这些字段名



编辑:如果您使用NET Framework 1.1的工作,那么你就没有办法AddWithValue,所以改变与两个AddWithValue线这些

  cmd.Parameters.Add(@第一,SqlDbType.NVarChar).value的= textbox2.text; 
cmd.Parameters.Add(@最后的,SqlDbType.NVarChar).value的= textbox3.text;

这代码假定你的数据库列类型为nvarchar的,如果不是,然后使用适当的SqlDbType枚举值。



请打算尽快切换到一个较新版本的.NET框架的。结果
1.1是真的过时了。


I am just a begginer on C# so i need too much help. Now the problem is that i have designed a windows form in which there are many fields like first name, last name, address etc. Now i want to do is that when i fill the form and click insert button all the information goes into database. Does anyone know how to do that?

private void button1_Click(object sender, System.EventArgs e)
{
    string connetionString = null;
    SqlConnection cnn ;
    SqlDataAdapter adapter = new SqlDataAdapter();
    string sql = null;
    connetionString = "Data Source=UMAIR;Initial Catalog=Air; Trusted_Connection=True;" ;

    cnn = new SqlConnection(connetionString);
    sql = "insert into Main (Firt Name, Last Name) values(textbox2.Text,textbox3.Text)";

    try
    {
        cnn.Open();
        adapter.InsertCommand = new SqlCommand(sql, cnn);
        adapter.InsertCommand.ExecuteNonQuery();
         MessageBox.Show ("Row inserted !! ");
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

解决方案

There are numerous problem in your query.
This is a modified version of your code

string connetionString = null;
string sql = null;
connetionString = "Data Source=UMAIR;Initial Catalog=Air; Trusted_Connection=True;" ;
using(SqlConnection cnn = new SqlConnection(connetionString))
{
   sql = "insert into Main ([Firt Name], [Last Name]) values(@first,@last)";
   cnn.Open();
   using(SqlCommand cmd = new SqlCommand(sql, cnn))
   {
       cmd.Parameters.AddWithValue("@first", textbox2.text);
       cmd.Parameters.AddWithValue("@last", textbox3.text);
       cmd.ExecuteNonQuery();
       MessageBox.Show ("Row inserted !! ");
   }
}

  • The column names contains spaces (this should be avoided) thus you need square brackets around them
  • You need to use the using statement to be sure that the connection will be closed and resources released
  • You put the controls directly in the string, but this don't work
  • You need to use a parametrized query to avoid quoting problems and sqlinjiection attacks
  • No need to use a DataAdapter for a simple insert query

Apart from this, there are other potential problems. What if the user doesn't input anything in the textbox controls? Do you have done any checking on this before trying to insert? As I have said the fields names contains spaces and this will cause inconveniences in your code. Try to change those field names.

EDIT: If you are working with NET Framework 1.1, then you don't have the method AddWithValue, so change the two AddWithValue lines with these

   cmd.Parameters.Add("@first", SqlDbType.NVarChar).Value = textbox2.text;
   cmd.Parameters.Add("@last", SqlDbType.NVarChar).Value = textbox3.text;

This code assumes that your database columns are of type NVARCHAR, if not, then use the appropriate SqlDbType enum value.

Please plan to switch to a more recent version of NET Framework as soon as possible.
The 1.1 is really obsolete now.

这篇关于如何使用C#语言中插入记录的数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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