我创建了一个将Excel文件转换为Access数据库的应用程序。在转换期间,MACAddress列中数据中的“:”符号需要用空格替换。
我尝试使用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
答案 0 :(得分:2)
由于我们没有本机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();
}
}
答案 1 :(得分:1)
我无法确定您尝试使用哪个字符串,但听起来您需要查看字符串方法。替换就像这样
"A:string".Replace(":", " ");
你能告诉我们你的替换是什么样的吗?