Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 替换为:&引用;用空格表示的符号(冒号)_C#_Excel_Ms Access - Fatal编程技术网

C# 替换为:&引用;用空格表示的符号(冒号)

C# 替换为:&引用;用空格表示的符号(冒号),c#,excel,ms-access,C#,Excel,Ms Access,我创建了一个将Excel文件转换为Access数据库的应用程序。在转换过程中,MACAddress列数据中的“:”符号需要替换为空格 我试图使用replace方法修改查询,但无效,它显示了一条错误消息: 未定义的函数“替换” 下面是我当前与replace函数一起使用的查询: cmd.CommandText = "INSERT INTO [MS Access;Database=" + Access + "].[NMS_List_Export] SELECT [IP Address] as [IPA

我创建了一个将Excel文件转换为Access数据库的应用程序。在转换过程中,MACAddress列数据中的“:”符号需要替换为空格

我试图使用replace方法修改查询,但无效,它显示了一条错误消息:

未定义的函数“替换”

下面是我当前与replace函数一起使用的查询:

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$]";
有人能在这个问题上帮助我吗。提前谢谢

请查找完整的代码:

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;

        }


    }




}
}

当前MAC地址列如下所示: 11:12:23:12 11:12:23:12 11:12:23:12 11:12:23:12 11:12:23:12 11:12:23:12

需要按以下方式转换: 11 12 23 12 11 12 23 12 11 12 23 12 11 12 23 12 11 12 23 12
11 12 23 12

我无法确定您要使用哪个字符串,但听起来您需要研究字符串方法。更换是这样做的

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

您能告诉我们您的替换是什么样子吗?

由于我们没有本机SQL替换方法,我担心您将不得不手动执行此操作:

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();
    }
}

@CD。。您的编辑导致语法错误。请共享导致错误的代码。。那么你可能会得到正确的答案而不是建议。谢谢你的回复。尝试使用给定的代码。但它给出的错误消息称为“无法加载引用类型库”Microsoft.Office.Interop.Access.Dao。“库未注册。(HRESULT的异常:0x8002801D(type_E_LIBNOTREGISTERED))。我添加了此引用:“Microsoft.Office.Interop.Access.Dao”。请help@hansikaattanayake这是一个完全不同的问题,与你原来的问题毫无关系。。在我看来,你最好把这个问题作为新问题发表。