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”是數據庫名稱。
Now, we will create a student table using "create table" command, in which we will insert the records.
現在,我們將使用“創建表”命令創建一個學生表,并在其中插入記錄。
In above table we created following columns,
在上表中,我們創建了以下幾列,
rollno | It is used to store roll number of student. |
name | It is used to store name of student. |
age | It is used to store age of student. |
class | It is used to store class of student in school. |
fee | It 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)
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.
并且我們使用了兩個類MySqlConnection和MySqlCommand ,在這里我們準備了連接數據庫的連接字符串。
"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,
現在看一下應用程序的輸出,
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數據庫。
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#中將集合寫入文本