I have created an application which converts Excel files to Access databases. During conversion the “:” symbol in data at the MACAddress column need to be replaced by a space.
I tried to modify the query by using replace method but it didn’t work, it shows an error message:
undefined function “replace”.
Given below is the query that I currently use with the replace function:
cmd.CommandText = "INSERT INTO [MS Access;Database=" + Access + "].[NMS_List_Export] SELECT [IP Address] as [IPAddress],Replace([Mac Address],':',' ') as [MACAddress],[Last seen on Channel] as [LastseenonChannel] FROM [NMS_List_Export$]";
Can somebody please help me on this issue. Thanks in advance!!
Please find the complete code:
namespace NMS_Client
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (File.Exists(@"C:\NMS_List_Export.mdb"))
{
File.Delete(@"C:\NMS_List_Export.mdb");
bool blnSuccess = CreateDB(@"C:\NMS_List_Export.mdb");
}
else
{
bool blnSuccess = CreateDB(@"C:\NMS_List_Export.mdb");
}
string Access = @"C:\NMS_List_Export.mdb";
string Excel = textBox1.Text.Replace("'\'", "'\\'");
string connect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Excel + ";Extended Properties=Excel 8.0;";
using (OleDbConnection conn = new OleDbConnection(connect))
{
using (OleDbCommand cmd = new OleDbCommand())
{
cmd.Connection = conn;
//Query is addressed
cmd.CommandText = "INSERT INTO [MS Access;Database=" + Access + "].[NMS_List_Export] SELECT [IP Address] as [IPAddress],[MAC Address]as [MACAddress],[Last seen on Channel] as [LastseenonChannel] FROM [NMS_List_Export$]";
conn.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("The import is complete!");
}
}
}
//CreateDB Method
public static bool CreateDB(string pstrDB)
{
try
{
Catalog cat = new Catalog();
string strCreateDB = "";
strCreateDB += "Provider=Microsoft.Jet.OLEDB.4.0;";
strCreateDB += "Data Source=" + pstrDB + ";";
strCreateDB += "Jet OLEDB:Engine Type=5";
cat.Create(strCreateDB);
Table nTable = new Table();
nTable.Name = "NMS_List_Export";
nTable.Columns.Append("IPAddress", DataTypeEnum.adVarWChar,25);
nTable.Columns.Append("MACAddress", DataTypeEnum.adVarWChar,25);
nTable.Columns.Append("LastseenonChannel", DataTypeEnum.adVarWChar,25);
cat.Tables.Append(nTable);
return true;
}
catch (Exception)
{
MessageBox.Show("The import is incomplete!");
throw;
}
}
}
}
Current MAC address column s as follows:
11:12:23:12
11:12:23:12
11:12:23:12
11:12:23:12
11:12:23:12
11:12:23:12
need to convert as below:
11 12 23 12
11 12 23 12
11 12 23 12
11 12 23 12
11 12 23 12
11 12 23 12
As we don’t have native SQL replace method, I fear you will have to do it manually: