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.

并且我们使用了两个类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,

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

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#中将集合写入文本

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

  1. java代码内创建mysql索引_点评阿里JAVA手册之MySQL数据库 (建表规约、索引规约、SQL语句、ORM映射)...

    下载原版阿里JAVA开发手册  [阿里巴巴Java开发手册v1.2.0] 本文主要是对照阿里开发手册,注释自己在工作中运用情况. 本文内容:MySQL数据库 (建表规约.索引规约.SQL语句.ORM映 ...

  2. c 获取mysql列数据_转 用C API 操作MySQL数据库

    用C API 操作MySQL数据库 参考MYSQL的帮助文档整理 这里归纳了C API可使用的函数,并在下一节详细介绍了它们.请参见25.2.3节,"C API函数描述". 函数 ...

  3. asp.net使用mysql教程_在C#程序中使用MYSQL数据库

    一提起mysql数据库,好像就是在说java,jsp或者php,其实在所有程序中都可以使用mysql 数据库,其它的程序使用my sql数据库的方法是使用my sql 提供的odbc驱动,在.net ...

  4. mysql数据库读写操作_一看就会,MySQL数据库的基本操作(二)

    上一节学习了MySQL数据库的基本操作的几个命令,1.登陆数据库:mysql -h localhost -u root -p.2.查看已有的数据库命令:show databases. 3.创建自己的数 ...

  5. mysql数据库用户简单分析_如何用SQLyog来分析MySQL数据库详解

    用SQLyog来分析MySQL数据库: SOLyog的下载.安装以及使用很简单.我去了相关网站下载,它只有384K字节大小.它把两个文件(一个可执行文件.exe和一个动态链接库文件.dll)安装到C: ...

  6. 织梦数据库改mysql前缀_织梦快速批量修改MYSQL数据库表前缀与表名实例操作

    这篇文章主要为大家详细介绍了织梦快速批量修改MYSQL数据库表前缀与表名实例操作,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,有需要的朋友可以收藏方便以后借鉴. 我们在安装织梦系统的时候,有一次 ...

  7. shell备份mysql思路_写一个shell脚本备份mysql数据库的步骤

    写一个shell脚本备份mysql数据库的步骤 发布时间:2020-05-25 15:47:41 来源:51CTO 阅读:221 作者:三月 下文我给大家简单讲讲关于写一个shell脚本备份mysql ...

  8. mysql数据库增加模块_使用bind-mysql模块增加对mysql数据库的支持

    对于bind,如果想修改或增加dns记录,是直接修改文本的,这样的话一方面不方面管理,另一方面容易出错,如果我们增加对mysql数据库的支持,再配合php或perl,python等程序直接操作mysq ...

  9. dms mysql定义变量_数据管理DMS:自建MySQL数据库 全量SQL诊断功能发布啦!-阿里云开发者社区...

    MySQL的用户都面临都一个难题,异常或者故障问题难定位,很多时候都靠"猜". 如果比较幸运,异常正在发生,我们还可以获取到会话.引擎状态等信息: 如果没有异常现场,要找到根因,除 ...

最新文章

  1. 机器学习_周志华_问题汇总_第1周
  2. python xlrd_python模块之xlrd
  3. 项目管理 - 学习总目录
  4. iOS之深入解析数组遍历的底层原理和性能分析
  5. 【活动】侬好上海,Microsoft Reactor来啦
  6. EBOOT跳转到NK的过程
  7. pta7-3 统计不及格人数_应用统计专硕院校专业难度分析【青岛大学、苏大、西安交通大学】...
  8. html1怎样插入视频,HTML视频教程,第1章 HTML初识
  9. android出现anr(application not response)的分析
  10. Android EditText 不得不说的InputFilter、TextWatcher、ActionMode.Callback、OnEditorActionListener
  11. 超级记忆/图像数字记忆 110位数字图像转换表 0-9
  12. QC4+充电协议_喜大普奔:高通推出QC3+/4+快充协议,这些处理器均支持
  13. 《人生七年》纪录片总结
  14. python 椭圆曲线dh密钥交换_密钥交换之DH算法
  15. python画双坐标图_Python双Y轴可视化
  16. 安卓10侧边返回_安卓10.0内测版出现新版手势操作:取消返回键、全靠Home胶囊完成...
  17. 安卓自动操作软件 AUTO.JS 4.1.1 ALPHA2 免费版下载
  18. 地方门户网站盈利的“达摩五指”
  19. web浏览器中的base64编码解码
  20. URP——着色器和材质——简单光照 Simple Lit

热门文章

  1. 微软MS11-050漏洞的利用
  2. 怎样呵护友谊_【家校联动共同呵护孩子健康成长科普课堂】关爱学生心理健康,守护学生健康成长...
  3. transform限制position:fixed的跟随效果
  4. js中var、let、const区别
  5. (转)用JS实现表格中隔行显示不同颜色
  6. Django操作与内容
  7. Python萌新笔记
  8. luoguP4213 【模板】杜教筛(Sum)杜教筛
  9. C#调用Power Shell 管理Office365 执行脚本时遇到的问题
  10. 数据结构及算法 -- 目录