zoukankan      html  css  js  c++  java
  • C#获取MySql 数据常用的代码

    1.读取

    public DataTable ExecuteDataTable(string SQLString)
    {
    using (MySqlConnection connection = new MySqlConnection(connectionString))
    {
    DataSet ds = new DataSet();
    try
    {
    connection.Open();
    MySqlCommand cmd = new MySqlCommand(SQLString, connection);
    MySqlDataAdapter command = new MySqlDataAdapter(cmd);
    command.Fill(ds, "ds");
    }
    catch (MySql.Data.MySqlClient.MySqlException ex)
    {
    throw new Exception(ex.Message);
    }
    return ds.Tables[0];
    }
    }

    2.使用SET IDENTITY_INSERT手动为数据库自增字段插入编号

        string sqlstr = "SET IDENTITY_INSERT t1 ON;";
                    lists.Add(sqlstr);
                    sqlstr = "INSERT INTO t1       (id, name) VALUES ("+i.ToString()+", '" + i.ToString()+"');";
                    lists.Add(sqlstr);
                    sqlstr = "set identity_insert t1 off;";
                    lists.Add(sqlstr);
    
                    da.SqlCeExecuteNonQuery(lists);

     public void SqlCeExecuteNonQuery(List<string> lists)
            {
                SqlCeConnection conn = new SqlCeConnection();
                SqlCeCommand comm = null;
                try
                {
                    conn.ConnectionString = PDAConnectionLocStr;
                    conn.Open();
                    comm = new SqlCeCommand("", conn);
                    for (int i = 0; i < lists.Count; i++)
                    {
                        comm.CommandText = lists[i];
                        comm.ExecuteNonQuery();
                    }
    
                }
                catch (SqlCeException ex)
                {
    
                }
                finally
                {
                    conn.Close();
                    conn.Dispose();
                    comm.Dispose();
                }
            }
     

    3.mysql数据Unix时间戳与C# DateTime时间类型互换

    dangranusing System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace WWFramework.DateTimes
    {
        /// <summary>
        /// 时间相关函数
        /// </summary>
        public static class Function
        {
            /// <summary>
            /// 将Unix时间戳转换为DateTime类型时间
            /// </summary>
            /// <param name="d">double 型数字</param>
            /// <returns>DateTime</returns>
            public static System.DateTime ConvertIntDateTime(double d)
            {
                 System.DateTime time = System.DateTime.MinValue;
                 System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
                 time = startTime.AddSeconds(d);
                return time;
             }
    
            /// <summary>
            /// 将c# DateTime时间格式转换为Unix时间戳格式
            /// </summary>
            /// <param name="time">时间</param>
            /// <returns>double</returns>
            public static double ConvertDateTimeInt(System.DateTime time)
            {
                double intResult = 0;
                 System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
                 intResult = (time - startTime).TotalSeconds;
                return intResult;
             }
         }
    }
  • 相关阅读:
    H5移动前端完美布局之-margin百分比的使用
    javascript 一个关于时间排序的算法(一个页面多个倒计时排序)
    html5 getComputedStyle + resize 实现动态宽高度等比响应式页面设计
    Windows 下关于转码的函数
    项目编译完之后自动提交
    eclipse 包含头文件出错
    Eclipse 包含头文件 添加环境变量
    linux下mongodb程序和c++客户端的编译
    简单谈谈消息发送缓冲区
    epoll 性能分析(解决占用CPU 过高问题)2
  • 原文地址:https://www.cnblogs.com/linyijia/p/5341444.html
Copyright © 2011-2022 走看看