public static int GetScalarValue(string queryString, CommandType commandType, IDataParameter[] dataParameter) { using (SqlConnection sqlConnection = new SqlConnection(GlobalMsADO.ConnectionString())) { SqlCommand sqlCommand = new SqlCommand(queryString, sqlConnection); sqlCommand.CommandType = commandType; if (dataParameter != null) { sqlCommand.Parameters.AddRange(dataParameter); } sqlConnection.Open(); int returnValue; object returnObject = sqlCommand.ExecuteScalar(); if (returnObject != DBNull.Value && returnObject != null && int.TryParse(returnObject.ToString(), out returnValue)) { return((int)returnValue); } else { return(-1); } } }
public static void UpdateNullableColumns(int dataMessageID, int splitDataMessageID) { using (SqlConnection sqlConnection = new SqlConnection(GlobalMsADO.ConnectionString())) { try { sqlConnection.Open(); //According to SONG THAN USER: This following code is used to update these NULLABLE column(s) of table DataMessageMaster (These NULLABLE column(s) are created by SONG THAN USER) DataSet dataSetDataMessageMaster = new DataSet(); SqlDataAdapter sqlDataAdapterDataMessage; SqlDataAdapter sqlDataAdapterSplitDataMessage; SqlCommandBuilder sqlCommandBuilder; sqlDataAdapterDataMessage = new SqlDataAdapter("SELECT * FROM DataMessageMaster WHERE DataMessageID = " + dataMessageID, sqlConnection); sqlDataAdapterDataMessage.Fill(dataSetDataMessageMaster, "DataMessageMaster"); //Fill the base datatable //Initialize the SqlDataAdapter object by specifying a Select command that retrieves data from the DataMessageMaster table. sqlDataAdapterSplitDataMessage = new SqlDataAdapter("SELECT * FROM DataMessageMaster WHERE DataMessageID = " + splitDataMessageID, sqlConnection); //Initialize the SqlCommandBuilder object to automatically generate and initialize the UpdateCommand, InsertCommand, and DeleteCommand properties of the SqlDataAdapter. sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapterSplitDataMessage); //Populate the DataSet by running the Fill method of the SqlDataAdapter. sqlDataAdapterSplitDataMessage.Fill(dataSetDataMessageMaster, "SplitDataMessageMaster"); bool hasUpdateNullableColumn = false; foreach (DataColumn dataColumn in dataSetDataMessageMaster.Tables["SplitDataMessageMaster"].Columns) { if (dataSetDataMessageMaster.Tables["SplitDataMessageMaster"].Rows[0].IsNull(dataColumn.ColumnName)) { dataSetDataMessageMaster.Tables["SplitDataMessageMaster"].Rows[0][dataColumn.ColumnName] = dataSetDataMessageMaster.Tables["DataMessageMaster"].Rows[0][dataColumn.ColumnName]; hasUpdateNullableColumn = true; } } if (hasUpdateNullableColumn) { sqlDataAdapterSplitDataMessage.Update(dataSetDataMessageMaster, "SplitDataMessageMaster"); //Post the data modification to the database. } } catch (Exception exception) { throw exception; } } }
public static int ExecuteNonQuery(string commandText, CommandType commandType) { Console.WriteLine(commandText); using (SqlConnection sqlConnection = new SqlConnection(GlobalMsADO.ConnectionString())) { try { sqlConnection.Open(); SqlCommand sqlCommand = new SqlCommand(commandText, sqlConnection); sqlCommand.CommandType = commandType; return(sqlCommand.ExecuteNonQuery()); } catch (Exception exception) { throw exception; } } }
/// <summary> /// GetDataTable /// </summary> /// <param name="queryString"></param> /// <returns></returns> public static DataTable GetDataTable(string queryString, CommandType commandType, IDataParameter[] dataParameter) { using (SqlConnection sqlConnection = new SqlConnection(GlobalMsADO.ConnectionString())) { SqlCommand sqlCommand = new SqlCommand(queryString, sqlConnection); sqlCommand.CommandType = commandType; if (dataParameter != null) { sqlCommand.Parameters.AddRange(dataParameter); } using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand)) { DataTable dataTable = new DataTable(); sqlDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey; sqlDataAdapter.Fill(dataTable); return(dataTable); } } }
public static int ExecuteTransaction(string commandText, CommandType commandType) { using (SqlConnection sqlConnection = new SqlConnection(GlobalMsADO.ConnectionString())) { int rowsAffected = -1; SqlTransaction sqlTransaction = null; try { sqlConnection.Open(); sqlTransaction = sqlConnection.BeginTransaction("MyTransaction"); //Begin add command here using (SqlCommand sqlCommand = new SqlCommand(commandText, sqlConnection, sqlTransaction)) { sqlCommand.CommandType = commandType; rowsAffected = sqlCommand.ExecuteNonQuery(); } //End add command here sqlTransaction.Commit(); return(rowsAffected); } catch (Exception exception) { try { sqlTransaction.Rollback(); throw exception; } catch (Exception rollbackException) { throw rollbackException; } } } }
public static int GetReturnValue(string queryString, CommandType commandType, IDataParameter[] dataParameter) { using (SqlConnection sqlConnection = new SqlConnection(GlobalMsADO.ConnectionString())) { SqlCommand sqlCommand = new SqlCommand(queryString, sqlConnection); sqlCommand.CommandType = commandType; if (dataParameter != null) { sqlCommand.Parameters.AddRange(dataParameter); } sqlConnection.Open(); SqlParameter returnParameter = sqlCommand.Parameters.Add("@ReturnValue", SqlDbType.Int); returnParameter.Direction = ParameterDirection.ReturnValue; sqlCommand.ExecuteNonQuery(); return((int)returnParameter.Value); } }
//public static string connectionString = "Server= .;Database= Northwind; Trusted_Connection= True"; public static SqlConnection Connection() { return(GlobalMsADO.MainDataAccessConnection()); ////////SqlConnection connection = new SqlConnection(connectionString); ////////return connection; }
public static int ExecuteNonQuery(string commandText, CommandType commandType) { return(ADODatabase.ExecuteNonQuery(commandText, commandType, GlobalMsADO.ConnectionString())); }
static void Main() { bool isLoginOK = false; ////KHONG LOAD DATABASE //GlobalVariables.LocationID = 1; //isLoginOK = true; try { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); GlobalRegistry.ShowError = true; //GlobalMsADO.ServerName = "SERVER\\SQLEXPRESS"; GlobalMsADO.ServerName = GlobalRegistry.Read("ServerName"); GlobalMsADO.DatabaseName = GlobalRegistry.Read("DatabaseName"); //GlobalMsADO.ServerName = "WH\\SQLEXPRESS"; //GlobalMsADO.DatabaseName = "E:\\20.02.2013\\Database\\BPFillingSystem.mdf"; //GlobalMsADO.ServerName = "HOME-PC\\SQLEXPRESS"; //GlobalMsADO.DatabaseName = "BPFillingSystem"; //GlobalMsADO.ServerName = "SONY-VIO\\SQLEXPRESS"; //GlobalMsADO.DatabaseName = "ImageS8SongThan"; GlobalMsADO.ServerName = "192.168.1.18"; while (!isLoginOK && GlobalVariables.LocationID >= -1)//Try to open startup database, Exit if user cancel to input ServerName and DatabaseName { //MessageBox.Show (GlobalStaticFunction.TextToASCII("AB")); //MessageBox.Show(GlobalStaticFunction.TextToHEX("AB")); ////byte[] a = GlobalStaticFunction.XORBytes(GlobalStaticFunction.StringToByteArrayFastest("01"), GlobalStaticFunction.StringToByteArrayFastest("02")); //////////string hex = BitConverter.ToString(data); //////////string hex = BitConverter.ToString(data).Replace("-", string.Empty); ; ////string hex = BitConverter.ToString(a); ////MessageBox.Show (hex); //////////1111111111---------MessageBox.Show(GlobalStaticFunction.TextToHEX("BOURG LES VALENCE" )); ////////////byte[] a = GlobalStaticFunction.CheckSumHEXString("0A,00,13,01,0A,02,38,49,4D,41,4A,45,20,02,54,46,52,41,4E,43,45,0D"); ////////////byte[] a = GlobalStaticFunction.CheckSumHEXString("0A,00,2A,01,0A,02,38,49,4D,41,4A,45,20,01,53,42,4F,55,52,47,20,4C,45,53,20,56,41,4C,45,4E,43,45,0A,02,54,46,52,41,4E,43,45,1E,1E,1E,0D"); //////////1111111111---------byte[] a = GlobalStaticFunction.CheckSumHEXString("0A,00,2A,01,0A,02,38,49,4D,41,4A,45,20,1C,01,53,42,4F,55,52,47,20,4C,45,53,20,56,41,4C,45,4E,43,45,0A,02,54,46,52,41,4E,43,45,1E,1E,1E,0D"); //////////1111111111---------MessageBox.Show(BitConverter.ToString(a)); if (GlobalMsADO.ServerName == null || GlobalMsADO.DatabaseName == null || GlobalMsADO.ServerName == "" || GlobalMsADO.DatabaseName == "")//Show Get Server Name Dialog { PublicGetServerName publicGetServerName = new PublicGetServerName(); if (publicGetServerName.ShowDialog() != DialogResult.OK) { GlobalVariables.LocationID = -2; } if (publicGetServerName.DialogResult == DialogResult.OK) { publicGetServerName.Dispose(); } } if (GlobalVariables.LocationID >= -1)//Do not execute if user cancel to input ServerName and DatabaseName { try { if (GlobalMsADO.MainDataAccessConnection(true).State == ConnectionState.Open)//Try to open new connection { isLoginOK = true; GlobalVariables.LocationID = 1; } else { GlobalMsADO.ServerName = ""; GlobalMsADO.DatabaseName = ""; } } catch { GlobalMsADO.ServerName = ""; GlobalMsADO.DatabaseName = ""; } } } if (GlobalVariables.LocationID > 0) { GlobalVariables.IgnoreEmptyCarton = GlobalRegistry.Read("IgnoreEmptyCarton") == "0" ? false : true; PublicApplicationLogon publicApplicationLogon = new PublicApplicationLogon(); if (publicApplicationLogon.ShowDialog() == DialogResult.OK) { Application.Run(new frmMDIMain()); } publicApplicationLogon.Dispose(); } } catch (Exception ex) { GlobalExceptionHandler.ShowExceptionMessageBox(new Form(), ex); } finally { Console.WriteLine("Executing finally block."); } }