Help! Why my code can not write string to MDB ?

G

Guest

i need your help, my code as below :


//-----------------------------------------------------------------------
//Wrote by Michael April 30 2005
//-----------------------------------------------------------------------

using System.Text;
using System.Diagnostics;
using System.Threading;
using System;
using System.Text.RegularExpressions;
using System.Data.OleDb;

public class LogInfo4
{
public static void Main(String[] args)
{

string log="System";
string machine="MyDC001";
EventLog aLog = new EventLog();
aLog.Log = log;
aLog.MachineName = machine;

Console.WriteLine("There are {0} entr[y|ies] in the log:",
aLog.Entries.Count);
//get string for print Message
foreach (EventLogEntry entry in aLog.Entries)
{

string strText = entry.Message;
string PrintDocNum,
PrintDocName,PrintUser,PrintServerName,PrintByte,PrintIp,PrintPages;
string strMatch = @"^Document (\d+), *(.+) owned by (.+) was printed on
(.*) via port IP_(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\. Size in bytes:
(\d+); pages printed: (\d+)";
Regex re = new Regex(strMatch);
Match m = re.Match(strText);
if(m.Success)
{
PrintDocNum = m.Groups[1].ToString();
PrintDocName = m.Groups[2].ToString();
PrintUser = m.Groups[3].ToString();
PrintServerName = m.Groups[4].ToString();
PrintIp = m.Groups[5].ToString();
PrintByte = m.Groups[6].ToString();
PrintPages = m.Groups[7].ToString();
OleDbConnection conn = new OleDbConnection();
// TODO: Modify the connection string and include any
// additional required properties for database.
conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
@"Data source= E:\April30\Db\TestDb.mdb" ;


conn.Open();
string sqlInsert=@"insert into
TestTable(DocNum,DocName,User,ServerName,Byte,Ip,Pages) values
(@PrintDocNum,@PrintDocName,@PrintUser,@PrintServerName,@PrintByte,@PrintIp,@PrintPages)";
OleDbCommand cmd1=new OleDbCommand(sqlInsert,conn);
cmd1.Parameters.Add("@PrintDocNum",System.Data.OleDb.OleDbType.VarChar,50,PrintDocNum);

cmd1.Parameters.Add("@PrintDocName",System.Data.OleDb.OleDbType.VarChar,50,PrintDocName);
cmd1.Parameters.Add("@PrintUser",System.Data.OleDb.OleDbType.VarChar,50,PrintUser);
cmd1.Parameters.Add("@PrintServerName",System.Data.OleDb.OleDbType.VarChar,50,PrintServerName);
cmd1.Parameters.Add("@PrintByte",System.Data.OleDb.OleDbType.VarChar,50,PrintByte);
cmd1.Parameters.Add("@PrintIp",System.Data.OleDb.OleDbType.VarChar,50,PrintIp);
cmd1.Parameters.Add("@PrintPages",System.Data.OleDb.OleDbType.VarChar,50,PrintPages);
//Console.WriteLine("Command Created");
cmd1.ExecuteNonQuery();//insert string
//Console.WriteLine("insert end");
conn.Close();
}

}
}
}
 
I

Ignacio Machin \( .NET/ C# MVP \)

hi

where r u getting the error?

did you try to insert the same values directly in access?

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

roopeman said:
i need your help, my code as below :


//-----------------------------------------------------------------------
//Wrote by Michael April 30 2005
//-----------------------------------------------------------------------

using System.Text;
using System.Diagnostics;
using System.Threading;
using System;
using System.Text.RegularExpressions;
using System.Data.OleDb;

public class LogInfo4
{
public static void Main(String[] args)
{

string log="System";
string machine="MyDC001";
EventLog aLog = new EventLog();
aLog.Log = log;
aLog.MachineName = machine;

Console.WriteLine("There are {0} entr[y|ies] in the log:",
aLog.Entries.Count);
//get string for print Message
foreach (EventLogEntry entry in aLog.Entries)
{

string strText = entry.Message;
string PrintDocNum,
PrintDocName,PrintUser,PrintServerName,PrintByte,PrintIp,PrintPages;
string strMatch = @"^Document (\d+), *(.+) owned by (.+) was printed on
(.*) via port IP_(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\. Size in bytes:
(\d+); pages printed: (\d+)";
Regex re = new Regex(strMatch);
Match m = re.Match(strText);
if(m.Success)
{
PrintDocNum = m.Groups[1].ToString();
PrintDocName = m.Groups[2].ToString();
PrintUser = m.Groups[3].ToString();
PrintServerName = m.Groups[4].ToString();
PrintIp = m.Groups[5].ToString();
PrintByte = m.Groups[6].ToString();
PrintPages = m.Groups[7].ToString();
OleDbConnection conn = new OleDbConnection();
// TODO: Modify the connection string and include any
// additional required properties for database.
conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
@"Data source= E:\April30\Db\TestDb.mdb" ;


conn.Open();
string sqlInsert=@"insert into
TestTable(DocNum,DocName,User,ServerName,Byte,Ip,Pages) values
(@PrintDocNum,@PrintDocName,@PrintUser,@PrintServerName,@PrintByte,@PrintIp,@PrintPages)";
OleDbCommand cmd1=new OleDbCommand(sqlInsert,conn);
cmd1.Parameters.Add("@PrintDocNum",System.Data.OleDb.OleDbType.VarChar,50,PrintDocNum);

cmd1.Parameters.Add("@PrintDocName",System.Data.OleDb.OleDbType.VarChar,50,PrintDocName);
cmd1.Parameters.Add("@PrintUser",System.Data.OleDb.OleDbType.VarChar,50,PrintUser);
cmd1.Parameters.Add("@PrintServerName",System.Data.OleDb.OleDbType.VarChar,50,PrintServerName);
cmd1.Parameters.Add("@PrintByte",System.Data.OleDb.OleDbType.VarChar,50,PrintByte);
cmd1.Parameters.Add("@PrintIp",System.Data.OleDb.OleDbType.VarChar,50,PrintIp);
cmd1.Parameters.Add("@PrintPages",System.Data.OleDb.OleDbType.VarChar,50,PrintPages);
//Console.WriteLine("Command Created");
cmd1.ExecuteNonQuery();//insert string
//Console.WriteLine("insert end");
conn.Close();
}

}
}
}
 
G

Guest

Hi,Ignacio, it 's end at "send error to Microsoft or not" pop menu, i can
insert the same
values to Mdb manually.and i am beginner in C#,Would please take a carefully
check
at my code?

Ignacio Machin ( .NET/ C# MVP ) said:
hi

where r u getting the error?

did you try to insert the same values directly in access?

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

roopeman said:
i need your help, my code as below :


//-----------------------------------------------------------------------
//Wrote by Michael April 30 2005
//-----------------------------------------------------------------------

using System.Text;
using System.Diagnostics;
using System.Threading;
using System;
using System.Text.RegularExpressions;
using System.Data.OleDb;

public class LogInfo4
{
public static void Main(String[] args)
{

string log="System";
string machine="MyDC001";
EventLog aLog = new EventLog();
aLog.Log = log;
aLog.MachineName = machine;

Console.WriteLine("There are {0} entr[y|ies] in the log:",
aLog.Entries.Count);
//get string for print Message
foreach (EventLogEntry entry in aLog.Entries)
{

string strText = entry.Message;
string PrintDocNum,
PrintDocName,PrintUser,PrintServerName,PrintByte,PrintIp,PrintPages;
string strMatch = @"^Document (\d+), *(.+) owned by (.+) was printed on
(.*) via port IP_(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\. Size in bytes:
(\d+); pages printed: (\d+)";
Regex re = new Regex(strMatch);
Match m = re.Match(strText);
if(m.Success)
{
PrintDocNum = m.Groups[1].ToString();
PrintDocName = m.Groups[2].ToString();
PrintUser = m.Groups[3].ToString();
PrintServerName = m.Groups[4].ToString();
PrintIp = m.Groups[5].ToString();
PrintByte = m.Groups[6].ToString();
PrintPages = m.Groups[7].ToString();
OleDbConnection conn = new OleDbConnection();
// TODO: Modify the connection string and include any
// additional required properties for database.
conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
@"Data source= E:\April30\Db\TestDb.mdb" ;


conn.Open();
string sqlInsert=@"insert into
TestTable(DocNum,DocName,User,ServerName,Byte,Ip,Pages) values
(@PrintDocNum,@PrintDocName,@PrintUser,@PrintServerName,@PrintByte,@PrintIp,@PrintPages)";
OleDbCommand cmd1=new OleDbCommand(sqlInsert,conn);
cmd1.Parameters.Add("@PrintDocNum",System.Data.OleDb.OleDbType.VarChar,50,PrintDocNum);

cmd1.Parameters.Add("@PrintDocName",System.Data.OleDb.OleDbType.VarChar,50,PrintDocName);
cmd1.Parameters.Add("@PrintUser",System.Data.OleDb.OleDbType.VarChar,50,PrintUser);
cmd1.Parameters.Add("@PrintServerName",System.Data.OleDb.OleDbType.VarChar,50,PrintServerName);
cmd1.Parameters.Add("@PrintByte",System.Data.OleDb.OleDbType.VarChar,50,PrintByte);
cmd1.Parameters.Add("@PrintIp",System.Data.OleDb.OleDbType.VarChar,50,PrintIp);
cmd1.Parameters.Add("@PrintPages",System.Data.OleDb.OleDbType.VarChar,50,PrintPages);
//Console.WriteLine("Command Created");
cmd1.ExecuteNonQuery();//insert string
//Console.WriteLine("insert end");
conn.Close();
}

}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top