如何将一些值添加到数据库中 [英] How can I add some values into my database

查看:82
本文介绍了如何将一些值添加到数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的初学者,我编写了一个连接到数据库的代码,但它给了我一个错误

I'm a beginner in C# and I wrote a code that connect to my database but It give me a error

我从一开始就做了所有事情,但没有任何反应

I did everything from first but nothing happened

private void btnSubmit_Click(object sender, EventArgs e) 
{
     string conString = "data source=DESKTOP-D5VFL9P; initial catalog = university; integrated security = True; MultipleActiveResultSets = True;";

     using (SqlConnection connection = new SqlConnection(conString)) 
     {
         connection.Open();

         using(SqlCommand command = new SqlCommand("INSERT INTO Persons (PersonID, LastName, FirstName, Age, City) VALUES (" + int.Parse(txtPersonID.Text) + ", '" +
    txtLastName.Text + "', '" + txtFirstName.Text + "' ," + int.Parse(txtAge.Text) + ", '" + txtCity.Text + "'", connection)) 
         {
             using(SqlDataReader reader = command.ExecuteReader()) 
             {
                  MessageBox.Show("Data inserted");

                  txtFirstName.Text = "";
                  txtLastName.Text = "";
                  txtPersonID.Text = "";
                  txtAge.Text = "";
                  txtCity.Text = "";
             }
         }
     }
 }

我想添加一些值到我的数据库

I want to add some values to my database

推荐答案

城市后面应该有。像 txtCity.Text +’)

这篇关于如何将一些值添加到数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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