用空格替换“:”符号(冒号)
Posted
技术标签:
【中文标题】用空格替换“:”符号(冒号)【英文标题】:Replace ":" symbol (colon) by a space 【发布时间】: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.. 您的编辑导致语法错误。 分享导致错误的代码..那么你可能会得到正确的答案而不是建议.. 【参考方案1】:由于我们没有原生的 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();
【讨论】:
嗨。谢谢回复。尝试使用给定的代码。但它给出了错误消息,称为“无法加载类型库以供参考”Microsoft.Office.Interop.Access.Dao“。未注册库。(来自 HRESULT 的异常:0x8002801D(TYPE_E_LIBNOTREGISTERED))”我添加了此参考:“Microsoft .Office.Interop.Access.Dao”。请帮忙 @hansikaattanayake 这是完全不同的问题,与您的原始问题无关。在我看来,您最好将此作为新问题发布。【参考方案2】:我无法确定您尝试使用哪个字符串,但听起来您需要研究字符串方法。替换是这样完成的
"A:string".Replace(":", " ");
你能告诉我们你的替换是什么样的吗?
【讨论】:
以上是关于用空格替换“:”符号(冒号)的主要内容,如果未能解决你的问题,请参考以下文章