.net链接access数据库的问题
我做了一个表单,然后点击提交protected void btnAddEassy_Click(object sender, EventArgs e)
{
string name = txtname.Text;
string ipone = txtipone.Text;
string add = txtadd.Text;
string home = txthome.Text;
string con = content1.Text;
DateTime dshijian = DateTime.Now;
int count = new DAL.PublicDAL().Excute("insert into online(name,ipnoe,add,home,con,dshijian) values('"+name+"','"+ipone+"','"+add+"','"+home+"','"+con+"','"+dshijian+"')");
if (count > 0)
{
Response.Write("<script>alert('感谢您的提交!')</script>");
}
else
{
Response.Write("<script>alert('提交异常!')</script>");
}
name,ipone, add,home,con,dshijian 都获取到了值,但是count=-1,sql语句检查了没问题,直接加数据insert into online(name,ipnoe,add,home,con,dshijian) values('2131','4324','52','452','425','2011-2-5')
还是count=-1,求大神解答这是什么问题? --------------------编程问答-------------------- 贴下你的Excute方法代码! --------------------编程问答--------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
namespace DAL
{
public class DBHelper
{
private static OleDbConnection connection;
public static OleDbConnection Connection
{
get
{
//从表示层的配置文件web.config中获取连接字符串
//string str = ConfigurationManager.ConnectionStrings["mydb"].ToString();
string str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.ConnectionStrings["mydb"].ToString());
if (connection == null)
{
connection = new OleDbConnection(str);
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Closed)
{
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Broken)
{
connection.Close();
connection.Open();
}
return connection;
}
}
// OleDbConnection conn = new OleDbConnection(ConfigurationManager.ConnectionStrings["mydb"].ToString());
//增,删,改
public static Int32 ExecuteCommand(string sql)
{
OleDbCommand cmd = new OleDbCommand(sql, Connection);
try
{
return cmd.ExecuteNonQuery();
}
catch
{
return -1;
}
finally
{
Connection.Close();
cmd.Dispose();
}
}
/// <summary>
/// 执行Select查询语句
/// </summary>
/// <param name="sql"></param>
/// <param name="values"></param>
/// <returns>返回数据表对象</returns>
public static DataTable GetDataTable(string sql, params OleDbParameter[] values)
{
//DataSet ds = new DataSet();
//using (OleDbDataAdapter da = new OleDbDataAdapter(sql, Connection))
//{
// da.Fill(ds);
//}
//return ds.Tables[0];
DataSet ds = new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter(sql, Connection);
try
{
//da.SelectCommand.Parameters.AddRange();
da.Fill(ds);
return ds.Tables[0];
}
catch
{
return null;
}
finally
{
da.Dispose();
}
}
//少量查询
public static OleDbDataReader GetReader(string sql)
{
OleDbCommand cmd = new OleDbCommand(sql, Connection);
return cmd.ExecuteReader();
}
public static object GetScalar(string commandText)
{
OleDbCommand cmd = new OleDbCommand(commandText, Connection);
try
{
return cmd.ExecuteScalar();
}
catch
{
return null;
}
finally
{
connection.Close();
cmd.Dispose();
}
}
}
}
--------------------编程问答--------------------
上面的是 DBH类,
public int Excute(string sql)
{
return DBHelper.ExecuteCommand(sql);
}
--------------------编程问答--------------------
OleDbCommand cmd = new OleDbCommand(sql, Connection);
try
{
return cmd.ExecuteNonQuery();
}
catch
{
return -1;
}
看到没!这里别返回-1 要关闭连接! --------------------编程问答-------------------- 你确认
你的sql语句能正常执行?
检查是否缺少字段或字段类型是否匹配 --------------------编程问答--------------------
恩,我重新设计了表,正常了
补充:.NET技术 , ASP.NET