c#中将集合写入文本_在C#中将记录插入MySQL数据库

本教程介绍了如何在C#应用程序中将学生记录存储到MySQL数据库。首先展示如何使用'显示数据库'和'使用mysqltest'命令选择数据库。接着创建名为'student'的表并插入记录。接着展示C#代码,包括使用MySqlConnection和MySqlCommand类来建立连接,准备插入命令并执行。最后,通过执行示例验证信息已成功保存到数据库。
摘要由CSDN通过智能技术生成

c#中将集合写入文本

In the last tutorial (how to connect with MySQL database in C#?), we learned about making the connection with MySQL database in C#. Here, in this tutorial, we will learn how to insert the records in MySQL database in C#?

在上一教程( 如何在C#中与MySQL数据库连接? )中,我们学习了如何在C#中与MySQL数据库建立连接。 在这里,在本教程中,我们将学习如何在C#中MySQL数据库中插入记录?

Here, we are creating a Windows Application, that will store student records into the database. So first of all, we will select the database using "show databases" and then change/use the database "use mysqltest", here "use mysqltest" is the database name.

在这里,我们正在创建一个Windows应用程序,它将学生记录存储到数据库中。 因此,首先,我们将使用“显示数据库”选择数据库,然后更改/使用“使用mysqltest”数据库,其中“使用mysqltest”是数据库名称。

Insert Records into MySQL database in C# | 1
Insert Records into MySQL database in C# | 2

Now, we will create a student table using "create table" command, in which we will insert the records.

现在,我们将使用“创建表”命令创建一个学生表,并在其中插入记录。

Insert Records into MySQL database in C# | 3

In above table we created following columns,

在上表中,我们创建了以下几列,

rollnoIt is used to store roll number of student.
nameIt is used to store name of student.
ageIt is used to store age of student.
classIt is used to store class of student in school.
feeIt is used to store fee amount
罗尔诺 用于存储学生的卷号。
名称 用于存储学生姓名。
年龄 用于存储学生的年龄。
它用于存储学生在学校的课程。
费用 用于存储费用金额

Now we will develop an application to store student information into database.

现在,我们将开发一个将学生信息存储到数据库中的应用程序。

C#应用程序将学生信息存储到MySQL数据库中 (C# application to store students information into MySQL database)

Insert Records into MySQL database in C# | 4

In the above application we changed following properties,

在上述应用程序中,我们更改了以下属性,

    Form
    Name	:	frmStuInfo
    Text	:	Student Information

    Label1
    Text	:	Roll Number

    Label2
    Text	:	Name

    Label3
    Text	:	Age

    Label4
    Text	:	Class

    Label5
    Text	:	Fee

    Button1
    Text 	:	Save To Database

用C#代码将记录插入MySQL数据库 (C# code to insert records into MySQL Database)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MySql.Data.MySqlClient;

namespace Insertion
{
    public partial class frmStuInfo : Form
    {
        public frmStuInfo()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MySqlConnection conn = new MySqlConnection("server=localhost;database=mysqltest;uid=root;pwd=root");

            MySqlCommand cmd = null;
            string cmdString = "";
            conn.Open();

            cmdString = "insert into student values(" + textBox1.Text + ",'" + textBox2.Text + "'," + textBox3.Text + ",'" + textBox4.Text + "'," + textBox5.Text + ")";

            cmd = new MySqlCommand(cmdString, conn);
            cmd.ExecuteNonQuery();

            conn.Close();

            MessageBox.Show("Data Stored Successfully");
        }
    }
}

Now In this above source code, we include a namespace for MySQL database connectivity is: "MySql.Data.MySqlClient".

现在,在上述源代码中,我们包括用于MySQL数据库连接的名称空间是: “ MySql.Data.MySqlClient”

And we used two classes MySqlConnection and MySqlCommand and here we prepare connection string to connect with database.

并且我们使用了两个类MySqlConnectionMySqlCommand ,在这里我们准备了连接数据库的连接字符串。

    "server=localhost;database=mysqltest;uid=root;pwd=root"

And we prepared insert command with inserted textbox values. And execute the command using the ExecuteNonQuery() method of MySqlCommand class. And finally, close connection and show appropriate message using MessageBox.

我们准备了带有插入的文本框值的插入命令。 然后使用MySqlCommand类ExecuteNonQuery()方法执行命令。 最后,关闭连接并使用MessageBox显示适当的消息。

Now look to the output of application,

现在看一下应用程序的输出,

Insert Records into MySQL database in C# | 5

In the above application, we filled information regarding a student and clicked on the command button the given information will be stored into the database. Now we will check information into the MYSQL database using MySQL prompt.

在上面的应用程序中,我们填写了有关学生的信息,然后单击命令按钮,将给定的信息存储到数据库中。 现在,我们将使用MySQL提示符将信息检入MYSQL数据库。

Insert Records into MySQL database in C# | 6

Here, we can see our information given by application is properly saved into database.

在这里,我们可以看到应用程序提供的信息已正确保存到数据库中。

翻译自: https://www.includehelp.com/dot-net/insert-records-into-mysql-database-in-csharp.aspx

c#中将集合写入文本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值