Home » excel » c# – Replace ":" symbol (colon) by a space

c# – Replace ":" symbol (colon) by a space

Posted by: admin May 14, 2020 Leave a comment

Questions:

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

How to&Answers:

As we don’t have native SQL replace method, I fear you will have to do it manually:

string strSelectSQL = "SELECT [IP Address] as [IPAddress], [MAC Address] as [MACAddress],[Last seen on Channel] as [LastseenonChannel] FROM [NMS_List_Export$]";
OleDbCommand selectCommand = new OleDbCommand(strSelectSQL, cmd.Connection);
OleDbParameter paramIP = new OleDbParameter("ip", "");
OleDbParameter paramMAC = new OleDbParameter("mac", "");
OleDbParameter paramLastSeen = new OleDbParameter("last_seen", "");
cmd.CommandText = "INSERT INTO [MS Access;Database=" + Access + "].[NMS_List_Export] VALUES (?, ?, ?)";
cmd.Parameters.Add(paramIP);
cmd.Parameters.Add(paramMAC);
cmd.Parameters.Add(paramLastSeen);
using (OleDbDataReader reader = selectCommand.ExecuteReader())
{
    while (reader.Read())
    {
        paramIP.Value = reader[0].ToString();
        paramMAC.Value = reader[1].ToString().Replace(":", " ");
        paramLastSeen.Value = reader[2].ToString();
        cmd.ExecuteNonQuery();
    }
}

Answer:

I can’t work out which string your trying to work on but it sounds like you need to look into string methods. A replace is done like this

"A:string".Replace(":", " ");

Can you show us what your replace looked like?