Access数据库的问题
这是我的代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;
namespace 我的日记本
{
class AccessHelper
{
public static string connstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=MyDiaryDB.mdb;Persist Security Info=False";
public static int ExecuteNonQuery(string sql)
{
OleDbConnection connetion = new OleDbConnection(connstring);
OleDbCommand command = new OleDbCommand(sql, connetion);
int result = 0;
try
{
connetion.Open();
result = command.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
connetion.Close();
}
return result;
}
public static int ExecuteScalar(string sql)
{
OleDbConnection connetion = new OleDbConnection(connstring);
OleDbCommand command = new OleDbCommand(sql, connetion);
int count = 0;
try
{
connetion.Open();
count = (int)command.ExecuteScalar();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
connetion.Close();
}
return count;
}
public static DataTable ExecuteTable(string sql)
{
OleDbConnection connetion = new OleDbConnection(connstring);
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(sql, connetion);
DataTable dataTable = new DataTable();
try
{
dataAdapter.Fill(dataTable);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return dataTable;
}
}
}
但登录的时候 报错:
未将对象引用设置到对象的实例 还有插入是出现 插入语句错误 这是插入语句 string sql = string.Format("insert into Diary(title,type,tianqi,time,content,UserId)values('{0}','{1}','{2}','{3}','{4}','{5}')", txtTitle.Text,comtype.Text,comtq.Text, dateTimePicker1.Value, richTextBox1.Text, UserHelper.userId);对不对呢
------解决方案--------------------
insert into [Diary]([title],[type],[tianqi],[time],[content],[UserId])
Access数据库sql语句中最好在用[]把表名和字段名框起来
LZ你登录报错把上面的数据库操作类贴出来又什么用?
出现“未将对象引用设置到对象的实例”是因为你登录的时候出现空值了,,登录一步步调试就可以知道了
------解决方案-------------------- 探讨 insert into [Diary]([title],[type],[tianqi],[time],[content],[UserId]) Access数据库sql语句中最好在用[]把表名和字段名框起来 LZ你登录报错把上面的数据库操作类贴出来又什么用? 出现“未将对象引用设置到对象的实例”是因为你登录的时候出现空值了,,登录一步步调试就可以知道了
------解决方案-------------------- 简单不容易出错的方法
string sql = "insert into Diary([title],[type],[tianqi],[time],[content],[Us