Linq tp sql 基础

本文详细介绍了 C# 3.0 中的 Linqtosql 技术,包括隐含类型局部变量、匿名类型、扩展方法、自动属性、对象初始化器和 Lambda 表达式等新特性,帮助开发者更好地理解和使用 LINQ 查询语法。

什么是Linq to sql

 

       Linq to sql(或者叫DLINQ)是LINQ(.NET语言集成查询)的一部分,全称基于关系数据的 .NET 语言集成查询,用于以对象形式管理关系数据,并提供了丰富的查询功能,它和Linq to xml、Linq to objects、Linq to dataset、Linq to entities等组成了强大的LINQ。

       要学好LINQ查询语法,就不得不先理解C# 3.0的一些新特性,下面一一简单介绍。

 

隐含类型局部变量

 

var age = 26;

var username = "zhuye";

var userlist = new [] {"a","b","c"};

foreach(var user in userlist)

Console.WriteLine(user);

       纯粹给懒人用的var关键字,告诉编译器(对于CLR来说,它是不会知道你是否使用了var,苦力是编译器出的),你自己推断它的类型吧,我不管了。但是既然让编译器推断类型就必须声明的时候赋值,而且不能是null值。注意,这只能用于局部变量,用于字段是不可以的。

 

匿名类型

 

var data = new {username = "zhuye",age = 26};

Console.WriteLine("username:{0} age:{1}", data.username, data.age);

匿名类型允许开发人员定义行内类型,无须显式定义类型。常和var配合使用,var用于声明匿名类型。定义一个临时的匿名类型在LINQ查询句法中非常常见,我们可以很方便的实现对象的转换和投影。

 

扩展方法

 

    public static class helper

    {

        public static string MD5Hash(this string s)

        {

            return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(s,"MD5");

        }

       

        public static bool In(this object o, IEnumerable b)

        {

            foreach(object obj in b)

            {

                if(obj==o)

                return true;

            }

            return false;

        }       

}

 

// 调用扩展方法

Console.WriteLine("123456".MD5Hash());

Console.WriteLine("1".In(new[]{"1","2","3"}));

很多时候我们需要对CLR类型进行一些操作,苦于无法扩展CLR类型的方法,只能创建一些helper方法,或者生成子类。扩展方法使得这些需求得意实现,同时也是实现LINQ的基础。定义扩展方法需要注意,只能在静态类中定义并且是静态方法,如果扩展方法名和原有方法名发生冲突,那么扩展方法将失效。

 

自动属性

 

    public class Person

    {

        public string username { get; protected set; }

        public int age { get; set; }

       

        public Person()

        {

            this.username = "zhuye";           

        }

}

 

Person p = new Person();

//p.username = "aa";

Console.WriteLine(p.username);

 

       意义不是很大,纯粹解决机械劳动。编译器自动为你生成get、set操作以及字段,并且你不能使用字段也不能自定义get、set操作,不过你可以分别定义get和set的访问级别。

 

对象初始化器

 

    public class Person

    {

        public string username { get; set; }

        public int age { get; set; }

       

        public override string  ToString()

        {

        return string.Format("username:{0} age:{1}", this.username, this.age);

        }

}

 

Person p = new Person() {username = "zhuye", age=26};

Console.WriteLine(p.ToString());

       编译器会自动为你做setter操作,使得原本几行的属性赋值操作能在一行中完成。这里需要注意:

l         允许只给一部分属性赋值,包括internal访问级别

l         可以结合构造函数一起使用,并且构造函数初始化先于对象初始化器执行

 

集合初始化器

 

    public class Person

    {

        public string username { get; set; }

        public int age { get; set; }

       

        public override string  ToString()

        {

        return string.Format("username:{0} age:{1}", this.username, this.age);

        }

}

 

var persons = new List<Person> {

    new Person {username = "a", age=1},

    new Person {username = "b", age=2}};

foreach(var p in persons)

Console.WriteLine(p.ToString());

编译器会自动为你做集合插入操作。如果你为Hashtable初始化的话就相当于使用了两个对象初始化器。

 

Lambda表达式

 

var list = new [] { "aa", "bb", "ac" };

var result = Array.FindAll(list, s => (s.IndexOf("a") > -1));

foreach (var v in result)

Console.WriteLine(v);

       其实和2.0中的匿名方法差不多,都是用于产生内联方法,只不过Lambda表达式的语法更为简洁。语法如下:

       (参数列表) => 表达式或者语句块

其中:

参数个数:可以有多个参数,一个参数,或者无参数。

表达式或者语句块:这部分就是我们平常写函数的实现部分(函数体)。

       前面的示例分别是1个参数的例子,下面结合扩展方法来一个复杂的例子:

      public delegate int mydg(int a, int b);

    

      public static class LambdaTest

      {

         

          public static int oper(this int a, int b, mydg dg)

          {

              return dg(a, b);

          }

      }

 

Console.WriteLine(1.oper(2, (a, b) => a + b));

Console.WriteLine(2.oper(1, (a, b) => a - b));

 

查询句法

 

var persons = new List<Person> {

    new Person {username = "a", age=19},

    new Person {username = "b", age=20},

    new Person {username = "a", age=21},

    };

var selectperson = from p in persons where p.age >= 20 select p.username.ToUpper();

foreach(var p in selectperson)

    Console.WriteLine(p);

查询句法是使用标准的LINQ查询运算符来表达查询时一个方便的声明式简化写法。该句法能在代码里表达查询时增进可读性和简洁性,读起来容易,也容易让人写对。Visual Studio 对查询句法提供了完整的智能感应和编译时检查支持。编译器在底层把查询句法的表达式翻译成明确的方法调用代码,代码通过新的扩展方法和Lambda表达式语言特性来实现。上面的查询句法等价于下面的代码:

var selectperson = persons.Where(p=>p.age>=20).Select(p=>p.username.ToUpper());

LINQ查询句法可以实现90%以上T-SQL的功能(由于T-SQL是基于二维表的,所以LINQ的查询语法会比T-SQL更简单和灵活),但是由于智能感应的原因,select不能放在一开始就输入。

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data.SqlClient; using System.Windows.Forms; namespace WStest { public class MySqlServer { SqlConnection mysqlCon; //配方信息结构体 public struct recipeNode { public int Tnnum; public int Tnclass; public int TnclassPrepar; public float Tntime; public float TntimePrepar; public float Tntp; public float TntpPrepar; public float Allowtpdif; public float AllowtpdifPrepar; public float Prhigh; public float PrhighPrepar; public float Prlow; public float PrlowPrepar; public float Arflow; public float ArflowPrepar; public float Highpr_MPa; public float Highpr_MPaPrepar; public float Fucnum; public float FucnumPrepar; } //配方信息结构体 public struct recipeInformNode { public int RecipeNum; public string Name; public string Marks; public DateTime DataTime; } public MySqlServer(string server, string integrated_Security, string initial_Catalog) { //server=可以写计算机名称 ip地址、Localhost或.都可以 //integrated security=SSPI 安全声明 //Intitial Catalog=csharpzx 初始化数据库 string constr = "Server=" + server + "; integrated security=" + integrated_Security + "; Initial Catalog=" + initial_Catalog; constr = "Server=DESKTOP-HMBL0P9\\SQLEXPRESS; integrated security=SSPI; Initial Catalog=wsdbtest01"; mysqlCon = new SqlConnection(constr); } public MySqlServer(string Server, string user, string pwd, string database) { //server=可以写计算机名称 ip地址、Localhost或.都可以 //user = sa 管理员名 //pwd=darly 安全声明 //database=csharpzx 初始化数据库 string constr = "Server=" + Server + "; user=sa" + user + "; pwd=" + pwd + ";database=" + database; constr = "Server=DESKTOP-HMBL0P9\\SQLEXPRESS; user=sa; pwd=AWSXCsqlws; database=wsdbtest01"; mysqlCon = new SqlConnection(constr); } public void openSqlServer() { string _msg = ""; try { //打开链接 mysqlCon.Open(); } catch (Exception ex) { _msg = ex.Message; //关闭链接 mysqlCon.Close(); } finally { if (_msg != "") { MessageBox.Show(_msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } public void closeSqlServer() { mysqlCon.Close(); } //读单条记录 public void readOne() { string sql = "select * from Table_ChartDate"; SqlCommand mycom = new SqlCommand(sql, mysqlCon); //声明dataReader对象 SqlDataReader mydr = mycom.ExecuteReader(); if (mydr.Read()) { /*******/ } else { MessageBox.Show("无数据"); } mydr.Close(); } //读取整表 public SqlDataReader readAll(string table) { string sql = "select * from " + table; SqlCommand mycom = new SqlCommand(sql, mysqlCon); //声明dataReader对象 SqlDataReader mydr = mycom.ExecuteReader(); return mydr; } //读单条记录 public SqlDataReader readOne_int(string table, string colName, string value) { string sql = "select * from " + table + " WHERE " + colName + "=" + value; SqlCommand mycom = new SqlCommand(sql, mysqlCon); //声明dataReader对象 SqlDataReader mydr = mycom.ExecuteReader(); return mydr; } //读单条记录 public SqlDataReader readOne_string(string table, string colName, string value) { string sql = "select * from " + table + " WHERE " + colName + "= " + "'" + value + "'"; SqlCommand mycom = new SqlCommand(sql, mysqlCon); //声明dataReader对象 SqlDataReader mydr = mycom.ExecuteReader(); return mydr; } //读第一条记录 public SqlDataReader readOneTop(string table) { string sql = "SELECT TOP 1 * FROM " + table + " ORDER BY id ASC"; SqlCommand mycom = new SqlCommand(sql, mysqlCon); //声明dataReader对象 SqlDataReader mydr = mycom.ExecuteReader(); return mydr; } //读最后一条记录 public SqlDataReader readOneBount(string table) { string sql = "SELECT TOP 1 * FROM " + table + " ORDER BY id DESC"; SqlCommand mycom = new SqlCommand(sql, mysqlCon); //声明dataReader对象 SqlDataReader mydr = mycom.ExecuteReader(); return mydr; } //批量读 public SqlDataReader readBufferDataRead(string table, string colName, string start, string end) { string sql = "select * from " + table + " WHERE " + colName + ">=" + "'" + start + "'" + " AND " + colName + "<=" + "'" + end + "'"; //sql = "select * from Table_AlarmInfo WHERE datatime>='2024-09-09 18:00:00.000' AND datatime<='2024-09-09 18:00:00.000'"; SqlCommand mycom = new SqlCommand(sql, mysqlCon); //声明dataReader对象 SqlDataReader mydr = mycom.ExecuteReader(); return mydr; } //单条写入报警数据 public void writeOne_AlarmInfo(string table, string code, string alarm_class, string alarm_info, string reason, string solution, string dataTime) { string sql = "INSERT INTO " + table + "(code, alarm_class, alarm_info, reason, solution, dataTime) VALUES(" + code + ", '" + alarm_class + "'" + ", '" + alarm_info + "'" + ", '" + reason + "'" + ", '" + solution + "'" + ", '" + dataTime + "'" + ")"; //string sql = "INSERT INTO Table_AlarmInfo(code, alarm_class, alarm_info, reason, solution, dataTime) VALUES(12, 'A', 'asfcve', 'wwww', 'gghhhg', '2024-09-11 09:22:00.000')"; SqlCommand mycom = new SqlCommand(sql, mysqlCon); mycom.ExecuteNonQuery(); } //单条写入测试曲线数据 public void writeOne_ChartDate(string table, string dataTime, double[] writeBuffer) { string sql = "INSERT INTO " + table + "(" + "datatime, " + "[加热区目标温度_℃], " + "[S1区-TT温度_℃], " + "[S1区-IP温度_℃], " + "[B区-TT温度_℃], " + "[B区-IP温度_℃], " + "[S2区-TT温度_℃], " + "[S2区-IP温度_℃], " + "[炉内相压_PT6.12_KPa], " + "[炉内绝压_PT6.13_KPa], " + "[炉内极限真空_PT6.13_Pa], " + "[脱蜡罐相压_PT9.12_KPa], " + "[脱蜡管绝压_PT9.13_KPa], " + "[炉内--脱蜡管压差_KPa], " + "炉内高压目标压力_MPa, " + "[炉内高压_PT11.6_MPa], " + "[脱蜡管气体温度_℃], " + "[脱蜡热水实际温度_℃], " + "[脱蜡热水设定温度_℃], " + "[前保温门温度_℃], " + "[后保温门温度_℃], " + "[冷却水_进水_℃], " + "[冷却水_出水_℃], " + "[S1区电极温度_℃], " + "[B区电极温度_℃], " + "[S2区电极温度_℃], " + "[炉前顶部温度_℃], " + "[炉前底部温度_℃], " + "[炉后顶部温度_℃], " + "[炉后底部温度_℃], " + "[预留1_℃], " + "[预留2_℃], " + "[冷却分离前端温度_℃], " + "[冷却分离后端温度_℃], " + "[N2目标流量_L/min], " + "[N2实际流量_L/min], " + "[S1区输出功率比_%], " + "[B区输出功率比_%], " + "[S2区输出功率比_%], " + "S1区输出功率_KW, " + "B区输出功率_KW, " + "S2区输出功率_KW, " + "S1区单炉累计能耗_KWH, " + "B区单炉累计能耗_KWH, " + "S2区单炉累计能耗_KWH, " + "单炉累计总能耗_KWH, " + "S1区漏电电流_A, " + "B区漏电电流_A, " + "S2区漏电电流_A, " + "S1区一次电压_V, " + "B区一次电压_V, " + "S2区一次电压_V, " + "S1区一次电流_A, " + "B区一次电流_A, " + "S2区一次电流_A, " + "[FIC11.26_L/min], " + "[FIC11.27_L/min], " + "[FIC11.28_L/min], " + "[FIC11.48_L/min], " + "[FIC11.47_L/min], " + "[FIC11.46_L/min], " + "[PT11.42_MPa], " + "[PT11.22_MPa], " + "[V8.4开度_%], " + "[V11.21开度_%]" + ") VALUES(" + "'" + dataTime + "'" + ", " + writeBuffer[0] + ", " + writeBuffer[1] + ", " + writeBuffer[2] + ", " + writeBuffer[3] + ", " + writeBuffer[4] + ", " + writeBuffer[5] + ", " + writeBuffer[6] + ", " + writeBuffer[7] + ", " + writeBuffer[8] + ", " + writeBuffer[9] + ", " + writeBuffer[10] + ", " + writeBuffer[11] + ", " + writeBuffer[12] + ", " + writeBuffer[13] + ", " + writeBuffer[14] + ", " + writeBuffer[15] + ", " + writeBuffer[16] + ", " + writeBuffer[17] + ", " + writeBuffer[18] + ", " + writeBuffer[19] + ", " + writeBuffer[20] + ", " + writeBuffer[21] + ", " + writeBuffer[22] + ", " + writeBuffer[23] + ", " + writeBuffer[24] + ", " + writeBuffer[25] + ", " + writeBuffer[26] + ", " + writeBuffer[27] + ", " + writeBuffer[28] + ", " + writeBuffer[29] + ", " + writeBuffer[30] + ", " + writeBuffer[31] + ", " + writeBuffer[32] + ", " + writeBuffer[33] + ", " + writeBuffer[34] + ", " + writeBuffer[35] + ", " + writeBuffer[36] + ", " + writeBuffer[37] + ", " + writeBuffer[38] + ", " + writeBuffer[39] + ", " + writeBuffer[40] + ", " + writeBuffer[41] + ", " + writeBuffer[42] + ", " + writeBuffer[43] + ", " + writeBuffer[44] + ", " + writeBuffer[45] + ", " + writeBuffer[46] + ", " + writeBuffer[47] + ", " + writeBuffer[48] + ", " + writeBuffer[49] + ", " + writeBuffer[50] + ", " + writeBuffer[51] + ", " + writeBuffer[52] + ", " + writeBuffer[53] + ", " + writeBuffer[54] + ", " + writeBuffer[55] + ", " + writeBuffer[56] + ", " + writeBuffer[57] + ", " + writeBuffer[58] + ", " + writeBuffer[59] + ", " + writeBuffer[60] + ", " + writeBuffer[61] + ", " + writeBuffer[62] + ", " + writeBuffer[63] + ")"; //string sql = "INSERT INTO Table_AlarmInfo(code, alarm_class, alarm_info, reason, solution, dataTime) VALUES(12, 'A', 'asfcve', 'wwww', 'gghhhg', '2024-09-11 09:22:00.000')"; SqlCommand mycom = new SqlCommand(sql, mysqlCon); mycom.ExecuteNonQuery(); } //单条写入测试曲线数据 public void writeOne_ChartDate02(string table, string dataTime, double[] writeBuffer) { string sql = "INSERT INTO " + table + "(" + "datatime, " + "设置温度, " + "底部热电偶温度, " + "顶部热电偶温度, " + "底部红外温度, " + "顶部红外温度, " + "保温桶温度, " + "循环水出口温度, " + "高压压力_KPaG, " + "绝压压力_KPa, " + "相压压力_KPaG, " + "真空度_Pa, " + "[N2质里流里计设置流里_L/Min], " + "[N2质量流量计反馈流里_L/Min], " + "[底部红外吹扫流里_L/Min], " + "[顶部红外吹扫流里_L/Min], " + "加热总功率_KWH, " + "底部调功器输出电流_A, " + "底部调功器输出电压_V, " + "[底部调功器PID输出值_%], " + "底部调功器输出功率_KW, " + "底部调功器用电量_KWH, " + "顶部调功器输出电流_A, " + "顶部调功器输出电压_v, " + "[顶部调功器PID输出值_%], " + "顶部调功器输出功率_KW, " + "顶部调功器用电量_KWH" + ") VALUES(" + "'" + dataTime + "'" + ", " + writeBuffer[0] + ", " + writeBuffer[1] + ", " + writeBuffer[2] + ", " + writeBuffer[3] + ", " + writeBuffer[4] + ", " + writeBuffer[5] + ", " + writeBuffer[6] + ", " + writeBuffer[7] + ", " + writeBuffer[8] + ", " + writeBuffer[9] + ", " + writeBuffer[10] + ", " + writeBuffer[11] + ", " + writeBuffer[12] + ", " + writeBuffer[13] + ", " + writeBuffer[14] + ", " + writeBuffer[15] + ", " + writeBuffer[16] + ", " + writeBuffer[17] + ", " + writeBuffer[18] + ", " + writeBuffer[19] + ", " + writeBuffer[20] + ", " + writeBuffer[21] + ", " + writeBuffer[22] + ", " + writeBuffer[23] + ", " + writeBuffer[24] + ", " + writeBuffer[25] + ")"; //string sql = "INSERT INTO Table_AlarmInfo(code, alarm_class, alarm_info, reason, solution, dataTime) VALUES(12, 'A', 'asfcve', 'wwww', 'gghhhg', '2024-09-11 09:22:00.000')"; SqlCommand mycom = new SqlCommand(sql, mysqlCon); mycom.ExecuteNonQuery(); } //单条写入测试数据 public void writeOneTest01(string table, string dataTime) { double test = 1; string sql = "INSERT INTO " + table + "(" + "datetime, " + "[加热区目标温度_℃], " + "[S1区-TT温度_℃], "+ "[S1区-IP温度_℃], "+ "[B区-TT温度_℃], "+ "[B区-IP温度_℃], "+ "[S2区-TT温度_℃], "+ "[S2区-IP温度_℃], "+ "[炉内相压_PT6.12_KPa], "+ "[炉内绝压_PT6.13_KPa], "+ "[炉内极限真空_PT6.13_Pa], "+ "[脱蜡罐相压_PT9.12_KPa], "+ "[脱蜡管绝压_PT9.13_KPa], "+ "[炉内--脱蜡管压差_KPa], "+ "炉内高压目标压力_MPa, "+ "[炉内高压_PT11.6_MPa], " + "[脱蜡管气体温度_℃], "+ "[脱蜡热水实际温度_℃], "+ "[脱蜡热水设定温度_℃], "+ "[前保温门温度_℃], "+ "[后保温门温度_℃], "+ "[冷却水_进水_℃], "+ "[冷却水_出水_℃], "+ "[S1区电极温度_℃], "+ "[B区电极温度_℃], "+ "[S2区电极温度_℃], "+ "[炉前顶部温度_℃], "+ "[炉前底部温度_℃], "+ "[炉后顶部温度_℃], "+ "[炉后底部温度_℃], " + "[预留1_℃], "+ "[预留2_℃], "+ "[冷却分离前端温度_℃], "+ "[冷却分离后端温度_℃], "+ "[N2目标流量_L/min], "+ "[N2实际流量_L/min], "+ "[S1区输出功率比_%], "+ "[B区输出功率比_%], "+ "[S2区输出功率比_%], " + "S1区输出功率_KW, "+ "B区输出功率_KW, "+ "S2区输出功率_KW, "+ "S1区单炉累计能耗_KWH, "+ "B区单炉累计能耗_KWH, "+ "S2区单炉累计能耗_KWH, "+ "单炉累计总能耗_KWH, "+ "S1区漏电电流_A, "+ "B区漏电电流_A, "+ "S2区漏电电流_A, "+ "S1区一次电压_V, "+ "B区一次电压_V, "+ "S2区一次电压_V, "+ "S1区一次电流_A, "+ "B区一次电流_A, "+ "S2区一次电流_A, "+ "[FIC11.26_L/min], "+ "[FIC11.27_L/min], "+ "[FIC11.28_L/min], "+ "[FIC11.48_L/min], "+ "[FIC11.47_L/min], "+ "[FIC11.46_L/min], "+ "[PT11.42_MPa], "+ "[PT11.22_MPa], "+ "[V8.4开度_%], "+ "[V11.21开度_%]" + ") VALUES(" + "'" + dataTime + "'" + ", " + test + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ", " + (test + 0.1) + ")"; //string sql = "INSERT INTO Table_AlarmInfo(code, alarm_class, alarm_info, reason, solution, dataTime) VALUES(12, 'A', 'asfcve', 'wwww', 'gghhhg', '2024-09-11 09:22:00.000')"; SqlCommand mycom = new SqlCommand(sql, mysqlCon); mycom.ExecuteNonQuery(); } //单条写入配方信息 public void writeOne_RecipeInform(string table, recipeInformNode recipeInformNode) { string sql = "INSERT INTO " + table + "(" + "recipeNum, " + "name, " + "marks, " + "dataTime" + ") VALUES(" + "'" + recipeInformNode.RecipeNum + "'" + ", " + recipeInformNode.Name + ", " + recipeInformNode.Marks + ", " + recipeInformNode.DataTime + ")"; //string sql = "INSERT INTO Table_AlarmInfo(code, alarm_class, alarm_info, reason, solution, dataTime) VALUES(12, 'A', 'asfcve', 'wwww', 'gghhhg', '2024-09-11 09:22:00.000')"; SqlCommand mycom = new SqlCommand(sql, mysqlCon); mycom.ExecuteNonQuery(); } //单条写入配方工艺 public void writeOne_Tnnum(string table, int recipeNum, string dataTime, recipeNode recipeNode) { string sql = "INSERT INTO " + table + "(" + "recipeNum, " + "dataTime, " + "tnNum, " + "tnclass, " + "tntime, " + "tntp, " + "allowtpdif, " + "prHigh, " + "PrLow, " + "arFlow, " + "highpr_MPa, " + "Fucnum" + ") VALUES(" + "'" + recipeNum + "'" + ", " + dataTime + ", " + recipeNode.Tnnum + ", " + recipeNode.Tnclass + ", " + recipeNode.Tntime + ", " + recipeNode.Tntp + ", " + recipeNode.Allowtpdif + ", " + recipeNode.Prhigh + ", " + recipeNode.Prlow + ", " + recipeNode.Arflow + ", " + recipeNode.Highpr_MPa + ", " + recipeNode.Fucnum + ")"; //string sql = "INSERT INTO Table_AlarmInfo(code, alarm_class, alarm_info, reason, solution, dataTime) VALUES(12, 'A', 'asfcve', 'wwww', 'gghhhg', '2024-09-11 09:22:00.000')"; SqlCommand mycom = new SqlCommand(sql, mysqlCon); mycom.ExecuteNonQuery(); } //单条写入配方信息 public void writeOne_RecipeInformTest(string table, string dataTime, string[] recipeInform) { string sql = "INSERT INTO " + table + "(" + "recipeNum, " + "name, " + "marks, " + "dataTime" + ") VALUES(" + recipeInform[0] + ", " + "'" + recipeInform[1] + "'" + ", " + "'" + recipeInform[2] + "'" + ", " + "'" + dataTime + "'" + ")"; //string sql = "INSERT INTO Table_AlarmInfo(code, alarm_class, alarm_info, reason, solution, dataTime) VALUES(12, 'A', 'asfcve', 'wwww', 'gghhhg', '2024-09-11 09:22:00.000')"; SqlCommand mycom = new SqlCommand(sql, mysqlCon); mycom.ExecuteNonQuery(); } //单条更新配方信息 public void updateOne_RecipeInform(string table, string dataTime, string colName, string value, string[] recipeInform) { string sql = "UPDATE " + table + " SET " + "name = " + "'" + recipeInform[0] + "'" + ", " + "marks = " + "'" + recipeInform[1] + "'" + ", " + "dataTime = " + "'" + dataTime + "'" + " WHERE " + colName + " = " + value; //string sql = "INSERT INTO Table_AlarmInfo(code, alarm_class, alarm_info, reason, solution, dataTime) VALUES(12, 'A', 'asfcve', 'wwww', 'gghhhg', '2024-09-11 09:22:00.000')"; //UPDATE Persons SET ID_P = 6, city = 'London' WHERE LastName = 'Wilson'; SqlCommand mycom = new SqlCommand(sql, mysqlCon); mycom.ExecuteNonQuery(); } //单条写入配方工艺 public void writeOne_TnnumTest(string table, int recipeNum, string dataTime, string[] recipe) { string sql = "INSERT INTO " + table + "(" + "recipeNum, " + "dataTime, " + "tnNum, " + "tnclass, " + "tntime, " + "tntp, " + "allowtpdif, " + "prHigh, " + "PrLow, " + "arFlow, " + "highpr_MPa, " + "Fucnum" + ") VALUES(" + recipeNum + ", " + "'" + dataTime + "'" + ", " + recipe[0] + ", " + recipe[1] + ", " + recipe[2] + ", " + recipe[3] + ", " + recipe[4] + ", " + recipe[5] + ", " + recipe[6] + ", " + recipe[7] + ", " + recipe[8] + ", " + recipe[9] + ")"; //string sql = "INSERT INTO Table_AlarmInfo(code, alarm_class, alarm_info, reason, solution, dataTime) VALUES(12, 'A', 'asfcve', 'wwww', 'gghhhg', '2024-09-11 09:22:00.000')"; SqlCommand mycom = new SqlCommand(sql, mysqlCon); mycom.ExecuteNonQuery(); } //更新单条配方工艺 public void updateOne_TnnumTest(string table, string dataTime, string colName, string value, string[] recipe) { string sql = "UPDATE " + table + " SET " + "dataTime = " + "'" + dataTime + "'" + ", " + "tnNum = " + recipe[0] + ", " + "tnclass = " + recipe[1] + ", " + "tntime = " + recipe[2] + ", " + "tntp = " + recipe[3] + ", " + "allowtpdif = " + recipe[4] + ", " + "prHigh = " + recipe[5] + ", " + "PrLow = " + recipe[6] + ", " + "arFlow = " + recipe[7] + ", " + "highpr_MPa = " + recipe[8] + ", " + "Fucnum = " + recipe[9] + " WHERE " + colName + " = " + value; //string sql = "INSERT INTO Table_AlarmInfo(code, alarm_class, alarm_info, reason, solution, dataTime) VALUES(12, 'A', 'asfcve', 'wwww', 'gghhhg', '2024-09-11 09:22:00.000')"; SqlCommand mycom = new SqlCommand(sql, mysqlCon); mycom.ExecuteNonQuery(); } //删除单条配方信息 public void deleteOne_RecipeInform(string table, string colName, string value) { string sql = "DELETE " + "FROM " + table + " WHERE " + colName + " = " + value; //string sql = "INSERT INTO Table_AlarmInfo(code, alarm_class, alarm_info, reason, solution, dataTime) VALUES(12, 'A', 'asfcve', 'wwww', 'gghhhg', '2024-09-11 09:22:00.000')"; SqlCommand mycom = new SqlCommand(sql, mysqlCon); mycom.ExecuteNonQuery(); } //删除配方工艺 public void delete_Tnnum(string table, string colName, string value) { string sql = "DELETE " + "FROM " + table + " WHERE " + colName + " = " + value; //string sql = "INSERT INTO Table_AlarmInfo(code, alarm_class, alarm_info, reason, solution, dataTime) VALUES(12, 'A', 'asfcve', 'wwww', 'gghhhg', '2024-09-11 09:22:00.000')"; SqlCommand mycom = new SqlCommand(sql, mysqlCon); mycom.ExecuteNonQuery(); } //添加报警 public void add_AlrmInfo(string table, string dataTime, int alarm_code, int alarm_class) { string sql = "INSERT INTO " + table + "(" + "dataTime, " + "alarm_code, " + "alarm_class" + ") VALUES(" + "'" + dataTime + "'" + ", " + alarm_code + ", " + alarm_class + ")"; //string sql = "INSERT INTO Table_AlarmInfo(code, alarm_class, alarm_info, reason, solution, dataTime) VALUES(12, 'A', 'asfcve', 'wwww', 'gghhhg', '2024-09-11 09:22:00.000')"; SqlCommand mycom = new SqlCommand(sql, mysqlCon); mycom.ExecuteNonQuery(); } } } 这是所有的MySqlServer类实现,请帮我实现IDisposable接口
最新发布
10-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值