| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1622 人关注过本帖
标题:无法写入数据库
取消只看楼主 加入收藏
师妃暄
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:805
专家分:107
注 册:2006-3-1
收藏
 问题点数:0 回复次数:3 
无法写入数据库

这是个简单的读写数据库的程序.编译没有问题.但是更新和新建的数据无法写入数据库.请大家指教

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Data.OleDb;
using System.Windows.Forms;

namespace _0070620_Data
{
public partial class Form1 : Form
{

OleDbConnection OleDbConnection1;
OleDbCommand OleDbCommand1;

OleDbDataAdapter OleDbDataAdapter1;
private bool bNewRecord = false; //表示是否处在插入新记录的状态

private void getCustomerID()
{//获取客户的ID
OleDbDataReader sdr;
OleDbConnection1.Open();
//执行SQL语句并返回
sdr = OleDbCommand1.ExecuteReader(CommandBehavior.CloseConnection);

cbxID.Items.Clear();
while (sdr.Read())
{
cbxID.Items.Add(sdr.GetValue(0));
}
sdr.Close();
cbxID.SelectedIndex = 0;
}
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“shiliDataSet.客户”中。您可以根据需要移动或移除它。
this.客户TableAdapter.Fill(this.shiliDataSet.客户);

String conn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=shili.mdb";
String sSQL = "Select * From 客户";
//创建一个数据库连接对象
OleDbConnection1 = new OleDbConnection(conn);
OleDbCommand1 = new OleDbCommand(sSQL, OleDbConnection1);

OleDbCommand1.CommandText = "Select 客户ID From 客户 Order By 客户ID";
OleDbDataAdapter1 = new OleDbDataAdapter(sSQL,OleDbConnection1);
//创建一个DataSet对象
DataSet DataSet1 = new DataSet();
OleDbDataAdapter1.Fill(DataSet1, "客户");
getCustomerID();
}

private void cbxID_SelectedIndexChanged(object sender, EventArgs e)
{
//创建SQL命令对象
OleDbCommand Olecmd = new OleDbCommand("Select * From 客户 where 客户ID=@客户ID", OleDbConnection1);
//设置参数
Olecmd.Parameters.AddWithValue("@客户ID",cbxID.Text);

OleDbDataReader sdr;
OleDbConnection1.Open();
sdr=Olecmd.ExecuteReader();

if (sdr.Read())
{
textBox2.Text = sdr.GetString(1);
textBox3.Text = sdr.GetString(2);
textBox4.Text = sdr.GetString(3);
textBox5.Text = sdr.GetString(5);
textBox6.Text = sdr["电话"].ToString(); //电话
textBox7.Text = sdr.GetString(8);
textBox8.Text = sdr.GetString(9);
textBox9.Text = sdr.GetString(4);
textBox10.Text = sdr["邮政编码"].ToString(); //邮政编码
textBox11.Text = sdr["传真"].ToString();//传真
}
sdr.Close();
OleDbConnection1.Close();
}

private void toolStripButton1_Click(object sender, EventArgs e)
{//前一条记录
if (cbxID.SelectedIndex > 0)
cbxID.SelectedIndex--;
else { MessageBox.Show("这是第一条记录", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); }

}

private void toolStripButton2_Click(object sender, EventArgs e)
{//后一条记录
if (cbxID.SelectedIndex < cbxID.Items.Count-1)
cbxID.SelectedIndex++;
else { MessageBox.Show("这是最后一条记录", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); }
}

private void toolStripButton3_Click(object sender, EventArgs e)
{//转到第一条记录
cbxID.SelectedIndex = 0;
}

private void toolStripButton4_Click(object sender, EventArgs e)
{//转到最后一条记录
cbxID.SelectedIndex = cbxID.Items.Count-1;
}

private void toolStripButton5_Click(object sender, EventArgs e)
{//新建记录
textBox2.Text ="";
textBox3.Text ="";
textBox4.Text ="";
textBox5.Text ="";
textBox6.Text ="";
textBox7.Text ="";
textBox8.Text ="";
textBox9.Text ="";
textBox10.Text ="";
textBox11.Text ="";
cbxID.DropDownStyle = ComboBoxStyle.DropDown;
cbxID.Text = "";
bNewRecord = false;

}

private void toolStripButton6_Click(object sender, EventArgs e)
{//保存记录
String OleDbStatement;
//根据是否正在添加新记录来建立适当的SQl语句
if (bNewRecord == false)
{
OleDbStatement = "Insert Into 客户 values(";
OleDbStatement += "'" + cbxID.Text + "',";
OleDbStatement += "'" + textBox2.Text + "',";
OleDbStatement += "'" + textBox3.Text + "',";
OleDbStatement += "'" + textBox4.Text + "',";
OleDbStatement += "'" + textBox9.Text + "',";
OleDbStatement += "'" + textBox5.Text + "',";
OleDbStatement += "'" + textBox10.Text + "',";
OleDbStatement += "'" + textBox6.Text + "',";
OleDbStatement += "'" + textBox7.Text + "',";
OleDbStatement += "'" + textBox8.Text + "',";
OleDbStatement += "'" + textBox11.Text + "')";
}
else
{
OleDbStatement = "Update 客户 Set";
OleDbStatement += "客户ID='" + cbxID.Text + "',";
OleDbStatement += "公司名称='" + textBox2.Text + "',";
OleDbStatement += "联系人姓名='" + textBox3.Text + "',";
OleDbStatement += "联系人头衔='" + textBox4.Text + "',";
OleDbStatement += "联系地址='" + textBox9.Text + "',";
OleDbStatement += "城市='" + textBox5.Text + "',";
OleDbStatement += "政编邮码='" + textBox10.Text + "',";
OleDbStatement += "电话='" + textBox6.Text + "',";
OleDbStatement += "地区='" + textBox7.Text + "',";
OleDbStatement += "国家='" + textBox8.Text + "',";
OleDbStatement += "传真='" + textBox11.Text + "'";
OleDbStatement += "where 客户ID='" + cbxID.Text + "'";
}
OleDbCommand OleDbcmd = new OleDbCommand(OleDbStatement, OleDbConnection1);
try
{
OleDbConnection1.Open();
int rowAffected = OleDbcmd.ExecuteNonQuery();
if (rowAffected == 1) //如果受影响的行数为1行
{
cbxID.Items.Add(cbxID.Text);
}
}
catch (Exception Err)
{
MessageBox.Show("更新错误:" + Err.Message, "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
OleDbConnection1.Close();
}
if (bNewRecord ==false)
{
cbxID.DropDownStyle = ComboBoxStyle.DropDownList;
bNewRecord = true;
cbxID.SelectedIndex = cbxID.Items.Count - 1;
}
}

}

}

图片附件: 游客没有浏览图片的权限,请 登录注册

搜索更多相关主题的帖子: 数据库 
2007-06-20 14:44
师妃暄
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:805
专家分:107
注 册:2006-3-1
收藏
得分:0 
编译没错误.就是写不进去

有实力才会有魅力 实力来自坚持不懈的努力
2007-06-20 15:22
师妃暄
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:805
专家分:107
注 册:2006-3-1
收藏
得分:0 
请大家帮忙啊

有实力才会有魅力 实力来自坚持不懈的努力
2007-06-21 10:05
师妃暄
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:805
专家分:107
注 册:2006-3-1
收藏
得分:0 
以下是引用guoxhvip在2007-6-21 21:34:35的发言:
说不定只是数据只写入到内存中 并没有持久化到数据库

就是这个问题啊.请问怎么解决


有实力才会有魅力 实力来自坚持不懈的努力
2007-06-23 01:37
快速回复:无法写入数据库
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021122 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved