请问各位
我有一个access的数据库,其中有字段userid,name,password,sex,re
我想往数据库中插如数据,这样做可以吗
private void add()
{
string myConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=e:usermanage.mdb";
OleDbConnection myConnection = new OleDbConnection(myConnectionString);
string myExecuteQuery="insert into userinfo (userid,name,password,sex,re) values('"+this.userid.Value+"','"+this.names.Value+"','"+this.psd1.Value+"','"+this.sex.SelectedValue+"','"+this.re.Value+")";
OleDbCommand myCommand = new OleDbCommand(myExecuteQuery);
myCommand.Connection = myConnection; myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
}
我有一个access的数据库,其中有字段userid,name,password,sex,re
我想往数据库中插如数据,这样做可以吗
private void add()
{
string myConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=e:usermanage.mdb";
OleDbConnection myConnection = new OleDbConnection(myConnectionString);
string myExecuteQuery="insert into userinfo (userid,name,password,sex,re) values('"+this.userid.Value+"','"+this.names.Value+"','"+this.psd1.Value+"','"+this.sex.SelectedValue+"','"+this.re.Value+")";
OleDbCommand myCommand = new OleDbCommand(myExecuteQuery);
myCommand.Connection = myConnection; myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
}
解决方案 »
- 请问谁有好“医院管理系统”文档!his系统,给我参考一下?
- 我现在要做一个文件管理器,刚学。net开发不久,大神们能不能给一点思路
- 关于DES加密的问题,请高手用C++把以下这段代码实现。
- 关于数组的问题,在线等,有答案就揭帖!
- 如何通过Type和mane取实例的值
- 高分求能很好展示数据层和业务层概念的文档或demo (C#,window Form 编程)
- 怎样获取WORD表内容的行号列号
- 求大神看看一段代码,急!!!!!!!!!
- c#的windows应用程序中为什么会有Form1.Designer.cs、Form1.cs、Program.cs三个文件????
- 急救:MSDN出问题了,目录和索引不能用了
- 地图X/Y问题 (雪中跪求解)
- 那里能查到 C#里小图标的含义阿?比如说什么是类?什么是事件?等
Dim mycom As New OleDbCommand("UPDATE T_Config SET PostAgeStandard =" & Me.TextBox1.Text & " WHERE (ID = 1)", conn)
conn.Open()
If mycom.ExecuteNonQuery > 0 Then
Me.Label1.Text = "收费标准已经更新成功,现为" & Me.TextBox1.Text & "元!"
End If
conn.Close()
web.config<appSettings> <add key="database" value="mdb.mdb" />
</appSettings>
Dim Conn As New OleDbConnection("Data Source=""" & Me.Server.MapPath(System.Configuration.ConfigurationSettings.AppSettings("database")) & """;Provider=""Microsoft.Jet.OLEDB.4.0"";User ID=Admin")
Conn.Close()
Dim ip As String = Me.Request.UserHostAddress
Dim body As String = Me.TextBox4.Text
Conn.Open()
Dim myCommand1 As New OleDbCommand("INSERT INTO T_GuestBook (GBName, GBEmail, GBTel, GBBody, GBIP) VALUES ('" & Me.TextBox1.Text & "','" & Me.TextBox2.Text & "','" & Me.TextBox3.Text & "','" & body & "','" & ip & "')", Conn)
If myCommand1.ExecuteNonQuery > 0 Then
Me.Label6.Text = "您的留言已经成功提交,谢谢您对我们的支持,请等候我们的回复!"
End If
Conn.Close()
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Data.OleDb.OleDbException: INSERT INTO 语句的语法错误。源错误:
行 74:
行 75: myCommand.Connection.Open();
行 76: myCommand.ExecuteNonQuery();
行 77: myConnection.Close();
行 78: }
--------------------
字段加个括号:
insert into userinfo ([userid],[name],[password],sex,re) values....
using System;
using System.Data;
using System.Data.OleDb;
using System.Xml;
namespace ACS
{
/// <summary>
/// ACES 的摘要描述。
/// 對數據進行基本的操作
/// </summary>
public class ACES
{
#region ACES 構造函數
public ACES()
{
}
#endregion #region 自定義變量
//Link DataBase String
private string sConString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source =ExTend.mdb ";
#endregion #region Return a OleDbConnection
/// <summary>
/// 返回一個數連接
/// </summary>
/// <returns>返回連接cn</returns>
private OleDbConnection Acs_cn()
{
OleDbConnection cn = new OleDbConnection(sConString);
if(cn.State == ConnectionState.Closed)
{
cn.Open();
}
return cn;
}
#endregion #region 查詢數據庫,返回查詢結果oDs
/// <summary>
/// 返回查詢結果
/// </summary>
/// <param name="sCmdString">查詢SQL字串</param>
/// <returns></returns>
public DataSet Retrieval(string sCmdString)
{
//建立連接
OleDbConnection cn = Acs_cn();
//建立Dataset對像
DataSet oDs = new DataSet();
try
{
//建立Adapter對像
OleDbDataAdapter myAdapter = new OleDbDataAdapter(sCmdString,cn);
myAdapter.Fill(oDs,"Result");
}
catch(Exception e)
{
throw e;
}
finally
{
cn.Close();
}
return oDs; }
#endregion #region 與數據庫進行交易
/// <summary>
/// 與數據庫進行交易 Succeed Return '1' Fail Return '-1'
/// </summary>
/// <param name="sCmdString">Operation SQL</param>
/// <returns></returns>
public int Translation(string sCmdString)
{
//定義操作變量,操作成功返回值為'1'
int nResult = -1;
//建立數據庫連接
OleDbConnection cn = Acs_cn();
//建立oledbcommand實例對像
OleDbCommand myCommand = new OleDbCommand(sCmdString,cn);
try
{
//執行操作
myCommand.ExecuteNonQuery();
nResult = 1;
}
catch(Exception e)
{
throw e;
}
finally
{
//關閉連接
cn.Close();
}
return nResult; }
#endregion } /// <summary>
/// 對SQL語句中用的參數進行格式化
/// </summary>
public class FormatSql
{
#region 構造函數
public FormatSql()
{
}
#endregion #region Format SQL string
public string SQLQ(string sText)
{
sText = "'"+sText+"'";
return sText;
} public string SQLQ(int n)
{
return n.ToString();
} public string SQLQ(double n)
{
return n.ToString();
} public string SQLQC(string sText)
{
sText = "'"+sText+"',";
return sText;
} public string SQLQC(int nText)
{
string sText = nText.ToString()+",";
return sText;
} public string SQLQC(double nText)
{
string sText = nText.ToString()+",";
return sText;
}
#endregion
}
}
private string sql = "";
private int nResult = -1;
private ACS.ACES cmnExec = new ACS.ACES();
private ACS.FormatSql cmnFormat = new FormatSql();
private DataSet oDs = new DataSet();
private DataSet oDs1 = new DataSet();
private int nWip_Id = -1;
public string sFactory_no = "";
public string sWip_No = "";
public string sPart_No = "";
public string sBox_No_Start = "";
public string sBox_Count = "";
public string sEcn_Ver = "";
public string sBios_Ver = "";
public int nModel = -1;
public string sLanguage = ""; #endregion #region 工單
public int Wip()
{
//判斷數據庫中是否存在此筆記錄,不存在則插入,存在則找出
//其對應的WIP_ID
sql = " SELECT * FROM WIP_INFO WHERE WIP_NO="+cmnFormat.SQLQ(sWip_No);
oDs = cmnExec.Retrieval(sql);
if(oDs.Tables[0].Rows.Count>0)
{
nWip_Id = int.Parse(oDs.Tables[0].Rows[0]["WIP_ID"].ToString().Trim());
nResult = nWip_Id;
}
else
{
sql = " INSERT INTO WIP_INFO";
sql += "(FACTORY_NO,WIP_NO,PART_NO,ECN_VER,BIOS_VER,BOX_INCEPT";
sql += ",BOX_SOURCE, CREATE_USER,CREATE_DATE,UPDATE_DATE)";
sql += "VALUES("+cmnFormat.SQLQC(sFactory_no)+cmnFormat.SQLQC(sWip_No)+cmnFormat.SQLQC(sPart_No);
sql += cmnFormat.SQLQC(sEcn_Ver)+cmnFormat.SQLQC(sBios_Ver);
sql += cmnFormat.SQLQC(sBox_No_Start)+cmnFormat.SQLQC(sBox_Count)+cmnFormat.SQLQC("ADMINISTRATOR");
sql += "Now(),Now())"; nResult = cmnExec.Translation(sql);
//插入成功後找出其對應的WIP_ID
if(nResult > 0)
{ sql = " SELECT * FROM WIP_INFO WHERE WIP_NO="+cmnFormat.SQLQ(sWip_No);
oDs = cmnExec.Retrieval(sql);
nWip_Id = int.Parse(oDs.Tables[0].Rows[0]["WIP_ID"].ToString().Trim());
nResult = nWip_Id;
}
}
return nWip_Id;
}
#endregion
比如+"','"+this.names.Value+"','"+不行,
要这样+"','"+" ' " + this.names.Value+ " ' " + "','"+
自己找到原因了
string myExecuteQuery="insert into userinfo (userid,name,password,sex,re) values('"+this.userid.Value+"','"+this.names.Value+"','"+this.psd1.Value+"','"+this.sex.SelectedValue+"','"+this.re.Value+")";'"+this.re.Value+"少了一个单引号。
呵呵