C#连接sqlserver数据库主要代码


```csharp
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace LabelFit
{
    
    
    public class SqlControl
    {
    
    
        private bool isInited = false;

        private SqlConnection sqlConnect = new SqlConnection();

        private object sqlLock = new object();

        public bool Connect()
        {
    
    
            if (isInited)
            {
    
    
                return true;
            }
            try
            {
    
    
                string serverName = "192.168.1.17,1433";
                string sqlName = "SecondEditionDatabase";
                string userName = "OP2-A";
                string userPw = "www.163.com";

                //Provider=SQLOLEDB.1;Password=www.163.com;Persist Security Info=True;User ID=OP2-A;Initial Catalog=SecondEditionDatabase;Data Source=192.168.1.17,1433\(local)
                //sqlConnect.ConnectionString = "Data Source=" + sqlConfig.serverName + ";Network Library = DBMSSOCN;Initial Catalog=" + sqlConfig.sqlName + ";User ID=" + sqlConfig.userName + ";PassWord=" + sqlConfig.userPw + ";Asynchronous Processing=True;MultipleActiveResultSets=True;Connect Timeout=10000";
                sqlConnect.ConnectionString = "Data Source=" + serverName + ";Initial Catalog=" + sqlName + ";User ID=" + userName + ";PassWord=" + userPw + ";";
                if (sqlConnect.State == ConnectionState.Closed)
                {
    
    
                    sqlConnect.Open();
                    isInited = true;
                }

            }
            catch (Exception ex)
            {
    
    

            }
            return isInited;
        }

        public void Close()
        {
    
    
            try
            {
    
    
                if (sqlConnect.State == ConnectionState.Open)
                {
    
    
                    sqlConnect.Close();
                    sqlConnect.Dispose();
                    isInited = false;
                }
            }
            catch (Exception ex)
            {
    
    
            }
        }

        public bool ReadLast(string tableName, out string[] dataOut)
        {
    
    
            lock (sqlLock)
            {
    
    
                dataOut = new string[0];

                if (isInited)
                {
    
    
                    for (int m = 0; m < 10; m++)
                    {
    
    
                        try
                        {
    
    
                            SqlCommand sqlCommand = new SqlCommand();
                            sqlCommand.CommandText = "SELECT top 1 * from " + tableName + " order by id desc";
                            SqlDataAdapter sqlAdapter = new SqlDataAdapter(sqlCommand.CommandText, sqlConnect);
                            DataSet dataInfo = new DataSet();
                            sqlAdapter.Fill(dataInfo);

                            if (dataInfo.Tables[0].Rows.Count > 0)
                            {
    
    
                                dataOut = new string[dataInfo.Tables[0].Rows[0].ItemArray.Length];
                                for (int i = 0; i < dataInfo.Tables[0].Rows[0].ItemArray.Length; i++)
                                {
    
    
                                    dataOut[i] = dataInfo.Tables[0].Rows[0].ItemArray[i].ToString();
                                }
                            }
                            sqlCommand.Dispose();
                            return true;
                        }
                        catch (Exception ex)
                        {
    
    
                            Thread.Sleep(10);
                            continue;
                        }
                    }
                }
                else
                {
    
    
                    return false;
                }
            }
            return false;
        }

        public bool InsertData(string tableName, string[] allHeads, string[] allData)
        {
    
    
            lock (sqlLock)
            {
    
    
                if (isInited)
                {
    
    
                    SqlCommand sqlCommand = new SqlCommand();
                    for (int m = 0; m < 10; m++)
                    {
    
    
                        try
                        {
    
    
                            string strCmd = "INSERT INTO " + tableName + " (";
                            for (int i = 0; i < allHeads.Length; i++)
                            {
    
    
                                strCmd += allHeads[i];
                                if (i != allHeads.Length - 1)
                                {
    
    
                                    strCmd += ",";
                                }
                            }
                            strCmd += ") VALUES ('";
                            for (int i = 0; i < allData.Length; i++)
                            {
    
    
                                DateTime timeValue;
                                if (allData[i].Contains("-") && DateTime.TryParse(allData[i], out timeValue))
                                {
    
    
                                    strCmd += timeValue;
                                }
                                else
                                {
    
    
                                    strCmd += allData[i];
                                }

                                if (i != allData.Length - 1)
                                {
    
    
                                    strCmd += "','";
                                }
                            }
                            strCmd += "')";
                            sqlCommand = new SqlCommand(strCmd, sqlConnect);
                            sqlCommand.ExecuteNonQuery();
                            sqlCommand.Dispose();
                            return true;
                        }
                        catch (Exception ex)
                        {
    
    
                            Thread.Sleep(10);
                            continue;
                        }
                    }
                }
                else
                {
    
    
                    return false;
                }
            }
            return false;
        }

        //根据conditionName = conditionValue 条件,查询dateHead列的值
        public bool ReadData(string tableName, string dateHead, string conditionName, string conditionValue, out DataSet dataInfo)
        {
    
    
            lock (sqlLock)
            {
    
    
                dataInfo = new DataSet();
                try
                {
    
    
                    if (isInited)
                    {
    
    
                        SqlCommand sqlCommand = new SqlCommand();

                        dataInfo.Clear();
                        string strCmd = "SELECT " + dateHead;

                        strCmd += " FROM " + tableName + " WHERE(" + conditionName + "='" + conditionValue + "')";

                        sqlCommand.CommandText = strCmd;
                        SqlDataAdapter sqlAdapter = new SqlDataAdapter(sqlCommand.CommandText, sqlConnect);
                        sqlAdapter.Fill(dataInfo);
                        sqlAdapter.Dispose();
                    }
                    else
                    {
    
    
                        return false;
                    }
                }
                catch (Exception ex)
                {
    
    
                    return false;
                }
            }
            return true;
        }


        public bool WriteData(string tableName, string[] allData,string conditionName, string conditionValue)
        {
    
    
            lock (sqlLock)
            {
    
    
                if (isInited)
                {
    
    
                    SqlCommand sqlCommand = new SqlCommand();
                    for (int m = 0; m < 10; m++)
                    {
    
    
                        try
                        {
    
    
                            string strCmd = "UPDATE " + tableName + " SET ";
                            for (int i = 0; i < allData.Length; i++)
                            {
    
    
                                strCmd += allData[i];
                                if (i != allData.Length - 1)
                                {
    
    
                                    strCmd += ",";
                                }
                            }
                            strCmd += " WHERE(" + conditionName + "='" + conditionValue + "')";
                            sqlCommand = new SqlCommand(strCmd, sqlConnect);
                            sqlCommand.ExecuteNonQuery();
                            sqlCommand.Dispose();
                            return true;
                        }
                        catch (Exception ex)
                        {
    
    
                            Thread.Sleep(10);
                            continue;
                        }
                    }
                }
                else
                {
    
    
                    return false;
                }
            }
            return false;
        }

    }

}

猜你喜欢

转载自blog.csdn.net/weixin_43935474/article/details/106587779
今日推荐