gpt4 book ai didi

c# - 用空格替换 ":"符号(冒号)

转载 作者:行者123 更新时间:2023-12-04 22:10:11 30 4
gpt4 key购买 nike

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

我尝试使用 replace 方法修改查询,但它不起作用,它显示一条错误消息:

undefined function "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地址列s如下:
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

最佳答案

由于我们没有本地 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();
}
}

关于c# - 用空格替换 ":"符号(冒号),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8693206/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com