c#中將集合寫入文本_在C#中將記錄插入MySQL數據庫

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#中將集合寫入文本

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/378921.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/378921.shtml
英文地址,請注明出處:http://en.pswp.cn/news/378921.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

read/fread write/fwrite 的區別

fread就是通過read來實現的,fread是C語言的庫,而read是系統調用。 差別在read每次讀的數據是調用者要求的大小,比如調用者要求讀取10個字節數據,read就會從內核緩沖區(操作系統開辟的一段空間用來存儲磁盤上的數據&am…

如何在子網中訪問上層網絡的計算機文件夾

場景 公司路由器A,直接接外部網線,內部ip192.168.11.1,lan口又接了路由器A1,IP為192.168.11.2,A1的lan端口接了一臺電腦A,Ip為192.168.0.2,接了另外一個路由A2,Ip為192.168.11.3&…

基于Web的套打方案分析

應用web化,不論對開發商,還是對用戶來說,實在是一種很經濟的選擇,因為基于web的應用,客戶端的規則很簡單,容易學習,容易維護,容易發布。但對程序員來說,因為瀏覽器的局限…

day1-Linux操作系統基礎

該專欄所有內容筆記均來自傳智播客培訓班 1.什么是操作系統(operate system OS) 小議:承上啟下作用,向下可以控制硬件,向上能夠支持軟件的運行。一個可以控制硬件的軟件。 小明找小紅聊天,小明打開QQ&…

關閉瀏覽器 清空session_跨境網絡小知識之Session

跨境小伙伴們大家好,上一篇為大家介紹了Cookie,今天就為大家介紹下連接cookie的另一端Session,交互過程中,二者缺一不可。與Cookie相對,Session是存儲在服務端的,他們之間是通過一個叫做sessionID的東東建立…

我和乘子交替方向法admm_找到最大和交替子序列

我和乘子交替方向法admmProblem statement: 問題陳述: Given a sequence of numbers, you have to find the maximum sum alternating subsequence and print the value. A sequence is an alternating sequence when it will be maintain like (increasing) ->…

Dojo學習筆記(一):Hello Dojo!

歡迎來到Dojo世界!在這篇文章中你將會學習到如何加載Dojo以及探索Dojo的一些核心功能。你還會了解Dojo的基于AMD的模塊架構,探索如何加載額外的模塊來增加功能到您的Web站點或應用程序,并找出在出錯的時如何得到幫助。讓我們開始吧 開始學習D…

轉:我眼中的Visual Studio 2010架構工具

來自:http://www.cnblogs.com/wayfarer/archive/2010/07/30/1788398.html我眼中的Visual Studio 2010架構工具影響架構質量的是構建體系架構的思想、原則、實踐與架構師的經驗,絕不是工具。即使是最優秀的架構工具,也不可能像倚天寶劍一般——…

VMware創建Ubuntu操作系統到網絡配置詳細流程

一、創建虛擬機 Ubuntu下載鏈接 1,看個人需求了,有更高的版本,下載Ubuntu鏡像 2,VMware官網隨便下載即可 3,創建新的虛擬機 4,自定義 5,默認即可 6,稍后安裝操作系統 7&#xf…

djiango配置mysql_數據庫MySQL相關環境配置以及數據庫與Go的連接

Linux下安裝好MySQL后,Windows安裝可視化工具navicatLinux下MySQL與Windows下navicat進行連接:安裝的過程很是揪心,各種查網站、大致把坑都寫了出來:1、在Linux下的mysql語句中,mysql> select host,user,authentication_string…

緩沖文件系統(fopen/fread/fwrite)和非緩沖文件系統(open/read/write)

open:系統調用,返回的是文件描述符,即文件句柄,是文件在文件描述副表里的索引。 fopen:C語言庫函數,返回的是一個指向文件結構的指針。fopen是ANSI C標準中的C語言庫函數,在不同的操作系統中應…

java 繼承示例_Java中的繼承類型以及示例

java 繼承示例Prerequisite: Inheritance and its implementation in Java 先決條件: 繼承及其在Java中的實現 Java中的繼承類型 (Type of inheritance in Java) In Java programming, there are following types of the inheritances, 在Java編程中,有…

基于HtmlParser的網絡爬蟲

一、 目標 獲取網頁中的超鏈接及鏈接名,如從http://www.hao123.com/開始,抓取所有hao123鏈接到的超鏈接,再以獲取到的鏈接網頁為目標,獲取它所鏈接到的網頁。 二、環境及開發工具 環境:Java 工具:MyEclip…

VMware下Ubuntu無法全屏顯示問題

一、運行Ubuntu的時候無法全屏顯示,如圖所示下載VMware Tools 二、之后將下載的文件拷貝到home文件夾下 三、解壓該壓縮包 由于該壓縮包是.tar.gz結尾的故壓縮命令:tar -zxvf VMwareTools-10.2.5-8068393.tar.gz,當然各版本有可能不一樣&am…

AMQP RabbitMQ

轉載:http://blog.ftofficer.com/2010/03/translation-rabbitmq-python-rabbits-and-warrens/官方介紹:http://www.rabbitmq.com/erlang-client-user-guide.html開始吧AMQP當中有四個概念非常重要:虛擬主機(virtual host&#xff…

fsync與fflush的關系和區別

read/write/fsync與fread/fwrite/fflush的關系和區別 read/write/fsync: linux底層操作; 內核調用, 涉及到進程上下文的切換,即用戶態到核心態的轉換,這是個比較消耗性能的操作。 fread/fwrite/fflush:…

lumanager mysql密碼_LuManager單獨安裝mysqli

首先確定你正在使用的php版本以及php.ini的位置,LuManager自帶了幾個版本。如果是默認安裝,應該是5.2.17。php.ini的位置應該是在/usr/local/php_fcgi/lib/php.ini要確定這些信息,可以自己編寫一個 info.phpphpinfo();?>把文件存放到網站…

數據庫系統數據庫管理系統_數據庫管理系統介紹

數據庫系統數據庫管理系統數據庫 (Database) A database is a collection of related data. In database any user can efficiently access the data which users want to retrieve. It can be anything from a simple collection of roll numbers, names, addresses and phone…

vba將select的值直接賦給變量

strSql ""strSql strSql & " select max(number) from dbo.#DATA" & vbCrLfrss.Open strSql, cnn numb rss.Fields(0)rss.Close轉載于:https://www.cnblogs.com/zigewb/archive/2013/02/06/2900645.html

set_exception_handler 自定義異常處理

剛才已經說過了set_error_handler這個函數,作用就是自定義錯誤處理, 那么現在就來簡單的說一下set_exception_handler,看名字我們就能發現,這說的是自定義異常處理。 呵呵,我聰明吧?來,先看一下…