public void InsertIntoDeviceInfoDB(string nodeid,string deviceinstance,string deviceip,string param1id,string param2id,string param1info,string param2info,string param1type,string param2type)
{
string databasePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string databaseFile = databasePath + @"\db_psychrometric_project.s3db";
string connString = @"Data Source=" + databaseFile + ";Version=3;";
string tableNameDevice = "tbl_" + selectedBuildingList[0].BuildingName + "_device_info_for_node";//currentNodeTableFromDB;
//checking if the data is present or not
SQLiteDataReader reader = null;
string queryString = "SELECT * from " + tableNameDevice + " WHERE nodeID = @id";
bool flag = false;
using (SQLiteConnection cxn = new SQLiteConnection(connString))
{
cxn.Open();
SQLiteCommand command = new SQLiteCommand(queryString, cxn);
command.Parameters.AddWithValue("@id", nodeid);//This is the group id that is used to identify each node
reader = command.ExecuteReader();
while (reader.Read())
{
if (reader["nodeID"].ToString() != "")
{
flag = true;
}
} //Close of while
} //Close of using
if (flag == false)
{
//insert the value
using (SQLiteConnection connection = new SQLiteConnection(connString))
{
connection.Open();
//SQLiteDataReader reader = null;
// MessageBox.Show("Insert the node value flag = false wala and id = "+nodeid);
string sql_string = "insert into " + tableNameDevice + "(nodeID,device_instanceID,IP,param1ID,param2ID,param1_info,param2_info,param1_identifier_type,param2_identifier_type) VALUES(@id,@instanceID,@IP,@param1,@param2,@param1info, @param2info, @param1_iden_type, @param2_iden_type)";
SQLiteCommand command = new SQLiteCommand(sql_string, connection);
command.CommandType = CommandType.Text;
command.Parameters.AddWithValue("@id", nodeid);
command.Parameters.AddWithValue("@instanceID", deviceinstance);
command.Parameters.AddWithValue("@IP", deviceip);
command.Parameters.AddWithValue("@param1", param1id);
command.Parameters.AddWithValue("@param2", param2id);
command.Parameters.AddWithValue("@param1info", param1info);
command.Parameters.AddWithValue("@param2info", param2info);
command.Parameters.AddWithValue("@param1_iden_type", param1type);
command.Parameters.AddWithValue("@param2_iden_type", param2type);
command.ExecuteNonQuery();
}
}
else
{
//Update the value
using (SQLiteConnection connection = new SQLiteConnection(connString))
{
connection.Open();
//SQLiteDataReader reader = null;
// MessageBox.Show("Update mechanism the node value flag = true wala,id = "+nodeid);
string sql_string = "UPDATE " + tableNameDevice + " SET device_instanceID = @instanceID,IP =@IP ,param1ID = @param1 ,param2ID = @param2,param1_info = @param1info,param2_info = @param2info,param1_identifier_type= @param1_iden_type,param2_identifier_type= @param2_iden_type where nodeID = @id ";
SQLiteCommand command = new SQLiteCommand(sql_string, connection);
command.CommandType = CommandType.Text;
command.Parameters.AddWithValue("@id", nodeid);
command.Parameters.AddWithValue("@instanceID", deviceinstance);
command.Parameters.AddWithValue("@IP", deviceip);
command.Parameters.AddWithValue("@param1", param1id);
command.Parameters.AddWithValue("@param2", param2id);
command.Parameters.AddWithValue("@param1info", param1info);
command.Parameters.AddWithValue("@param2info", param2info);
command.Parameters.AddWithValue("@param1_iden_type", param1type);
command.Parameters.AddWithValue("@param2_iden_type", param2type);
command.ExecuteNonQuery();
}
}
}