• 大小: 350KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-05-23
  • 语言: C#
  • 标签: C#  数据库  面向对象  

资源简介

C#连接mysql数据库的工程文件,基本实现增删改查的操作,不能直接运行,要改一下数据库的连接,对初学C#的开发人员会有很大的帮助。

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Configuration;
using MySql.Data;
using MySql.Data.MySqlClient;
using ConsoleApplication3;

namespace Sql
{
    class Program
    {
        public static void Main(string[] args)
        {
            //    //【1】封装数据库的连接字符串,并创建连接对象
            //    string constructorString = “server=localhost;User Id=root;password=lwd19980322;Database=myemployees“;
            //    //调用MySqlConnection含参的构造方法
            //    MySqlConnection myConnnect = new MySqlConnection(constructorString);
            //    //【2】创建Command对象
            //    //MySqlCommand myCmd = new MySqlCommand();
            //    //myCmd.CommandText = constructorString;
            //    //myCmd.Connection = myConnnect;
            //    //string sql = “insert into jobs(job_idjob_titlemin_salarymax_salary)“;
            //    //sql += “ values(‘AD钙奶‘‘ad钙奶总经理‘50000100000)“; 
            //    //string sql = “select job_id= count(*)from jobs“; 
            //    string sql = “select job_idjob_titlemin_salary from jobs“;
            //    //调用含两个参数的构造方法(Mysql命令,连接对象)
            //    MySqlCommand myCmd = new MySqlCommand(sql myConnnect);
            //    //【3】打开数据库连接
            //    myConnnect.Open();
            //    //【4】执行数据操作,返回受影响的行数(int)
            //    //int result = myCmd.ExecuteNonQuery();
            //    //Console.WriteLine(“受影响的行数=“+result);
            //    ////执行这个方法后,返回的是单行单列
            //    //object result = myCmd.ExecuteScalar();
            //    ////执行一个查询,返回一个多行多列的结果集,即是一个对象,这个结果并没有给客户端,而是通过SqlDataReader对象
            //   MySqlDataReader reader = myCmd.ExecuteReader();
            //    //循环读取结果Read()方法返回的是一个bool值
            //    while(reader.Read())
            //    {
            //        //使用索引器读取
            //        Console.WriteLine(reader[0]+“\t“ +reader[1]+“\t“+reader[2] );
            //    }

            //    //【5】关闭连接,首先关闭读取器对象,再关闭Command对象、Connect对象
            //    reader.Close();
            //    myCmd.Dispose();
            //    myConnnect.Close();
            //    Console.ReadLine();
            //}
            string sql = “select job_idjob_titlemin_salary from jobs“;
            MySqlDataReader reader = SQLhelper.GetReader(sql);
            while (reader.Read())
            {
                //使用索引器读取
                Console.WriteLine(reader[0] + “\t“ + reader[1] + “\t“ + reader[2]);
            }
            Console.ReadLine();
        }
    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-05-23 13:13  ConsoleApplication3\
     目录           0  2018-05-23 13:13  ConsoleApplication3\.vs\
     目录           0  2018-05-23 13:13  ConsoleApplication3\.vs\ConsoleApplication3\
     目录           0  2018-05-23 13:13  ConsoleApplication3\.vs\ConsoleApplication3\v14\
     文件       46080  2018-05-26 09:18  ConsoleApplication3\.vs\ConsoleApplication3\v14\.suo
     目录           0  2018-05-26 08:58  ConsoleApplication3\ConsoleApplication3\
     文件        1024  2018-05-23 13:13  ConsoleApplication3\ConsoleApplication3.sln
     目录           0  2018-05-23 13:13  ConsoleApplication3\ConsoleApplication3\bin\
     目录           0  2018-05-25 07:57  ConsoleApplication3\ConsoleApplication3\bin\Debug\
     文件        6656  2018-05-26 08:58  ConsoleApplication3\ConsoleApplication3\bin\Debug\ConsoleApplication3.exe
     文件       15872  2018-05-26 08:58  ConsoleApplication3\ConsoleApplication3\bin\Debug\ConsoleApplication3.pdb
     文件       22688  2018-05-26 08:58  ConsoleApplication3\ConsoleApplication3\bin\Debug\ConsoleApplication3.vshost.exe
     文件         490  2017-09-29 21:43  ConsoleApplication3\ConsoleApplication3\bin\Debug\ConsoleApplication3.vshost.exe.manifest
     文件      423936  2016-06-17 23:34  ConsoleApplication3\ConsoleApplication3\bin\Debug\MySql.Data.dll
     文件      336896  2016-06-17 23:34  ConsoleApplication3\ConsoleApplication3\bin\Debug\MySql.Data.Entity.dll
     文件      348672  2016-06-17 23:34  ConsoleApplication3\ConsoleApplication3\bin\Debug\MySql.Data.Entity.EF6.dll
     文件       93696  2016-06-17 23:34  ConsoleApplication3\ConsoleApplication3\bin\Debug\MySql.Web.dll
     文件        3096  2018-05-25 09:52  ConsoleApplication3\ConsoleApplication3\ConsoleApplication3.csproj
     目录           0  2018-05-23 13:13  ConsoleApplication3\ConsoleApplication3\obj\
     目录           0  2018-05-25 08:32  ConsoleApplication3\ConsoleApplication3\obj\Debug\
     文件        1157  2018-05-26 08:58  ConsoleApplication3\ConsoleApplication3\obj\Debug\ConsoleApplication3.csproj.FileListAbsolute.txt
     文件       30694  2018-05-25 08:32  ConsoleApplication3\ConsoleApplication3\obj\Debug\ConsoleApplication3.csprojResolveAssemblyReference.cache
     文件        6656  2018-05-26 08:58  ConsoleApplication3\ConsoleApplication3\obj\Debug\ConsoleApplication3.exe
     文件       15872  2018-05-26 08:58  ConsoleApplication3\ConsoleApplication3\obj\Debug\ConsoleApplication3.pdb
     文件        6712  2018-05-25 08:56  ConsoleApplication3\ConsoleApplication3\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
     目录           0  2018-05-23 13:13  ConsoleApplication3\ConsoleApplication3\obj\Debug\TempPE\
     文件        3027  2018-05-26 08:58  ConsoleApplication3\ConsoleApplication3\Program.cs
     目录           0  2018-05-23 13:13  ConsoleApplication3\ConsoleApplication3\Properties\
     文件        1346  2018-05-23 13:13  ConsoleApplication3\ConsoleApplication3\Properties\AssemblyInfo.cs
     文件        4343  2018-05-25 13:57  ConsoleApplication3\ConsoleApplication3\SQLhelper.cs

评论

共有 条评论