|
|
|
ขอวิธี insert ข้อมูล ลงฐานข้อมูล OLEDB กับ C# (.NET) |
|
|
|
|
|
|
|
ไม่รู้จักฐานข้อมูลยี่ห้อ OleDb รู้จักแต่ที่มันเป็น provider น่ะ
Code (C#)
// สมมุติว่าติดต่อ access
string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydatabase.mdb;";
OleDbConnection Conn = new OleDbConnection(strConn);
string strCommand = "Insert Into [MyTable] ([MyNVarChar], [MySmallDateTime]) Value (@MyNVarChar, @MySmallDateTime)";
OleDbCommand Command = new OleDbCommand(strCommand, Conn);
Command.Parameters.AddWithValue("@MyNVarChar", "your data");
Command.Parameters.AddWithValue("@MySmallDateTime", DateTime.Today);
try
{
Conn.Open();
Command.ExecuteNonQuery();
Conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
|
|
|
|
|
Date :
2010-06-05 20:01:32 |
By :
tungman |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอบคุนค่ะ
|
|
|
|
|
Date :
2010-06-05 20:03:33 |
By :
sajok |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code ((C#) ASP.NET System.Data.OleDb - ExecuteNonQuery())
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.OleDb"%>
<%@ Page Language="C#" Debug="true" %>
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
//Sample1();
Sample2();
}
void Sample1()
{
System.Data.OleDb.OleDbConnection objConn;
System.Data.OleDb.OleDbCommand objCmd;
String strConnString,strSQL;
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
Server.MapPath("database/mydatabase.mdb")+";Jet OLEDB:Database Password=;";
objConn = new System.Data.OleDb.OleDbConnection(strConnString);
objConn.Open();
strSQL = "INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) " +
"VALUES ('C005','Weerachai Nukitram','[email protected]','TH','2000000','1000000')";
objCmd = new System.Data.OleDb.OleDbCommand();
objCmd.Connection = objConn;
objCmd.CommandType = CommandType.Text;
objCmd.CommandText = strSQL;
objCmd.ExecuteNonQuery();
lblText.Text = lblText.Text + "- Record Inserted";
objCmd = null;
objConn.Close();
objConn = null;
}
void Sample2()
{
System.Data.OleDb.OleDbConnection objConn;
System.Data.OleDb.OleDbCommand objCmd;
String strConnString,strSQL;
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
Server.MapPath("database/mydatabase.mdb")+";Jet OLEDB:Database Password=;";
objConn = new System.Data.OleDb.OleDbConnection(strConnString);
objConn.Open();
strSQL = "INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) " +
"VALUES ('C005','Weerachai Nukitram','[email protected]','TH','2000000','1000000')";
objCmd = new System.Data.OleDb.OleDbCommand(strSQL,objConn);
try
{
objCmd.ExecuteNonQuery();
lblText.Text = lblText.Text + "- Record Inserted";
}
catch(Exception ex)
{
lblText.Text = lblText.Text + "<br> - Record not insert was (" + ex.Message + ")";
}
objCmd = null;
objConn.Close();
objConn = null;
}
</script>
<html>
<head>
<title>ThaiCreate.Com ASP.NET - System.Data.OleDb</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label id="lblText" runat="Server"></asp:Label>
</form>
</body>
</html>
|
|
|
|
|
Date :
2010-06-06 07:42:50 |
By :
webmaster |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|