c# 向MySQL添加數據的兩種方法
下面介紹兩種執行SQL命令的方法,并作出相應地總結,第一種介紹一種常規用法,下面進行做簡要地分析,首先我們需要執行打開數據庫操作首先創建一個MySqlConnection對象,在其構造函數中傳入一個連接字符串,然后執行Open操作打開數據庫,在正確打開數據庫之后我們才能進行相關的動作,在ExecuteSQL這個函數中,
我們執行MySqlCommand myCmd = new MySqlCommand(CmdString, conn),從而創建MySqlCommand對象,其中傳入的兩個參數分別為sql命令和第一步建立的MySqlConnection對象,然后執行int Cmd = myCmd.ExecuteNonQuery()這一句執行相應的命令,并返回受影響的行數,最后我們需要關閉數據庫連接,并釋放非托管資源,從而完成整個數據庫操作的過程;這個是比較常規的一種做法,也是我們使用比較多的一種形式。
public class DataBaseMySqlHelper{ string connstr; MySqlConnection conn; //Server=xxxxxxx;Database=xxxxxxx;Uid=xxxxxxx;Pwd=xxxxxxx;CharSet=gbk; //Server=xxx.xx.xxx.xx;Database=MSUP;Uid=dvision;Pwd=dvision;Port=xxxx;allow zero datetime = true; MainWindow _MainWindow; public DataBaseMySqlHelper(MainWindow mainWindow) {_MainWindow = mainWindow; connstr = _MainWindow._ConfigInfo.MySqlConnectionStrings; } public DataBaseMySqlHelper(MainWindow mainWindow, string connectionString) {_MainWindow = mainWindow;connstr = connectionString; } /// <summary> /// 打開數據庫連接 /// </summary> void Open() {try{ conn = new MySqlConnection(connstr); conn.Open();}catch (Exception ex){ System.Windows.Forms.MessageBox.Show(ex.Message);} } /// <summary> /// 關閉數據庫連接 /// </summary> void Close() {conn.Close(); } /// <summary> /// 返回影響數據庫的行數/// </summary> /// <param name='CmdString'></param> /// <returns></returns> public int ExecuteSQL(string CmdString) {try{ Open(); MySqlCommand myCmd = new MySqlCommand(CmdString, conn); int Cmd = myCmd.ExecuteNonQuery(); Close(); return Cmd;}catch (Exception ex){ _MainWindow.ShowErrorMessage(' MySql數據庫查詢失敗!'); return 0;} } /// <summary> /// 返回數據表 /// </summary> /// <param name='CmdString'></param> /// <returns></returns> public DataTable GetDataTable(string CmdString) {try{ Open(); DataSet myDs = new DataSet(); MySqlDataAdapter myDa = new MySqlDataAdapter(); myDa.SelectCommand = new MySqlCommand(CmdString, conn); myDa.Fill(myDs); Close(); return myDs.Tables[0];}catch (Exception e){ _MainWindow.ShowErrorMessage(' MySql數據庫查詢失敗!'); return null;} }}
第二種方式,這里也貼出關鍵代碼并做簡要的分析:這里的關鍵是MySqlParameter[]數組的使用,我們在執行SQL語句的時候 DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);這里的parameters就是MySqlParameter[]數組,里面存儲的是每個字段的數據,我們將在下面的代碼中展示parameters的內容,
/// <summary>/// 增加一條數據/// </summary>public void Add(Maticsoft.Model.cameradetail model){ StringBuilder strSql=new StringBuilder(); strSql.Append('insert into cameradetail_gis('); strSql.Append('EncodeDeviceUsername,MatrixIP,EncodeDevicePassword,id,Name,DisplayName,Location,Longitude,Latitude,CameraActive,ServerID,ForwardSvrIP,ForwardSvrPort,EncodeDeviceIP,EncodeDevicePort,CameraType,CameraModel,DeviceChannel,MatrixPort,MatrixChannelNum,VideoStoreServerIP,VideoStoreServerPort,VideoStoreServerUserID,VideoStoreServerPassword,EncodeDeviceModelNum,EncodeDeviceModelName,CameraInstallAddress,CameraStatus)'); strSql.Append(' values ('); strSql.Append('@EncodeDeviceUsername,@MatrixIP,@EncodeDevicePassword,@id,@Name,@DisplayName,@Location,@Longitude,@Latitude,@CameraActive,@ServerID,@ForwardSvrIP,@ForwardSvrPort,@EncodeDeviceIP,@EncodeDevicePort,@CameraType,@CameraModel,@DeviceChannel,@MatrixPort,@MatrixChannelNum,@VideoStoreServerIP,@VideoStoreServerPort,@VideoStoreServerUserID,@VideoStoreServerPassword,@EncodeDeviceModelNum,@EncodeDeviceModelName,@CameraInstallAddress,@CameraStatus)'); MySqlParameter[] parameters = { new MySqlParameter('@EncodeDeviceUsername', MySqlDbType.VarChar,64), new MySqlParameter('@MatrixIP', MySqlDbType.VarChar,16), new MySqlParameter('@EncodeDevicePassword', MySqlDbType.VarChar,64), new MySqlParameter('@id', MySqlDbType.VarChar,14), new MySqlParameter('@Name', MySqlDbType.VarChar,100), new MySqlParameter('@DisplayName', MySqlDbType.VarChar,100), new MySqlParameter('@Location', My
相關文章:
