【发布时间】:2012-01-01 13:00:45
【问题描述】:
我创建了一个将 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地址列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
【问题讨论】:
-
@CD.. 您的编辑导致语法错误。
-
分享导致错误的代码..那么你可能会得到正确的答案而不是建议..