c# - 我在数据集中有两个数据表。我通常登录和退出。当我回去时,其中一个数据表是空的
问题描述
我将用户信息存储在“用户”表中。我将这些用户生成的条形码信息保存在“条形码”表中。每次“用户”内容为空。我找不到解决方案。当其中一个数据表被删除时,另一个可以正常工作。创建单独的数据集更准确。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;
using System.Threading.Tasks;
namespace Ulgen_QA_Barcode
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
static UserDataSet db;
protected static UserDataSet App
{
get
{
if (db == null)
db= new UserDataSet();
return db;
}
}
string skullanici;
string spass;
string slevel;
int yetki=0;
private void Form1_Load(object sender, EventArgs e)
{
String fileNameb = string.Format("{0}//userdata.dat", Application.StartupPath);
if (File.Exists(fileNameb))
App.Barcode.ReadXml(fileNameb);
barcodeBindingSource.DataSource = App.Barcode;
string fileName = string.Format("{0}//userdata.dat", Application.StartupPath);
if (File.Exists(fileName))
App.Users.ReadXml(fileName);
usersBindingSource.DataSource = App.Users;
dataGridView2.DataSource = barcodeBindingSource;
dataGridView1.DataSource = usersBindingSource;
//panel1.Enabled = false;
//BCode.Enabled = false;
//BCode.Visible = false;
//panel3.Enabled = false;
//panel3.Visible = false;
//button2.Enabled = false;
//button2.Visible = false;
//button3.Enabled = false;
//button3.Visible = false;
//button5.Enabled = false;
//button5.Visible = false;
}
private void Kayit_Click(object sender, EventArgs e)
{
try
{
usersBindingSource.EndEdit();
App.Users.AcceptChanges();
App.Users.WriteXml(string.Format("{0}//userdata.dat", Application.StartupPath));
panel1.Enabled = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Mesaj", MessageBoxButtons.OK, MessageBoxIcon.Error);
App.Users.RejectChanges();
}
}
private void Degistir_Click(object sender, EventArgs e)
{
panel1.Enabled = true;
textBox1.Focus();
}
private void Iptal_Click(object sender, EventArgs e)
{
usersBindingSource.ResetBindings(false);
panel1.Enabled = false;
}
private void Yeni_Click(object sender, EventArgs e)
{
try
{
panel1.Enabled = true;
App.Users.AddUsersRow(App.Users.NewUsersRow());
usersBindingSource.MoveLast();
textBox1.Focus();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Mesaj", MessageBoxButtons.OK, MessageBoxIcon.Error);
App.Users.RejectChanges();
}
}
private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
if (MessageBox.Show("Kayıt Silinecek", "Mesaj", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
usersBindingSource.RemoveCurrent();
}
}
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
{
// ARAMA
//if (e.KeyChar == (char)13)
//{
// if (!string.IsNullOrEmpty(textBox4.Text))
// {
// var query = from o in App.Users
// where o.User == textBox4.Text || o.User.Contains(textBox4.Text)
// select o;
// dataGridView1.DataSource = query.ToList();
// }
// else
// {
// dataGridView1.DataSource = usersBindingSource;
// }
//}
}
private void BT_Giris_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(Kullanici.Text))
{
var query = from o in App.Users
where o.User == Kullanici.Text //|| o.User.Contains(Kullanici.Text)
select o;
dataGridView1.DataSource = query.ToList();
}
else
{
dataGridView1.DataSource = usersBindingSource;
}
if (dataGridView1.SelectedCells.Count > 0)
{
int selectedrowindex = dataGridView1.SelectedCells[0].RowIndex;
DataGridViewRow selectedRow = dataGridView1.Rows[selectedrowindex];
// textBox5.Text = Convert.ToString(selectedRow.Cells["Pass"].Value);
skullanici = dataGridView1.Rows[0].Cells[1].Value.ToString();
spass = dataGridView1.Rows[0].Cells[2].Value.ToString();
slevel = dataGridView1.Rows[0].Cells[3].Value.ToString();
if (Kullanici.Text == skullanici & Sifre.Text == spass)
{
label2.Text = dataGridView1.Rows[0].Cells[3].Value.ToString();
if (slevel == "User")
{
yetki = 1;
kullanıcı();
}
if (slevel == "Admin")
{
yetki = 2;
}
if (yetki == 2)
{
button2.Visible = true;
button2.Enabled = true;
kullanıcı();
}
}
else
{
MessageBox.Show("Kullanıcı veya Şifre Hatalı", "Uyarı", MessageBoxButtons.OK, MessageBoxIcon.Error);
skullanici = "";
spass = "";
slevel = "";
Kullanici.Text = "";
Sifre.Text = "";
label2.Text = "";
dataGridView1.DataSource = usersBindingSource;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
yetki = 0;
skullanici = "";
spass = "";
slevel = "";
Kullanici.Text = "";
Sifre.Text = "";
label2.Text = "";
BCode.Visible = true;
dataGridView1.DataSource = usersBindingSource;
dataGridView2.DataSource = barcodeBindingSource;
}
void kullanıcı()
{
button5.Visible = true;
button5.Enabled = true;
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
panel3.Visible = true;
panel3.Enabled = true;
dataGridView1.DataSource = usersBindingSource;
dataGridView2.DataSource = barcodeBindingSource;
}
private void button4_Click(object sender, EventArgs e)
{
textBox5.Text = BCode.Text;
textBox6.Text = Kullanici.Text;
textBox7.Text = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss");
try
{
barcodeBindingSource.EndEdit();
App.Barcode.AcceptChanges();
App.Barcode.WriteXml(string.Format("{0}//userdata.dat", Application.StartupPath));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Mesaj", MessageBoxButtons.OK, MessageBoxIcon.Error);
App.Barcode.RejectChanges();
}
try
{
usersBindingSource.EndEdit();
App.Users.AcceptChanges();
App.Users.WriteXml(string.Format("{0}//userdata.dat", Application.StartupPath));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Mesaj", MessageBoxButtons.OK, MessageBoxIcon.Error);
App.Users.RejectChanges();
}
string barcode = BCode.Text;
Bitmap bitmap = new Bitmap(barcode.Length * 40, 150);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
Font oFont = new System.Drawing.Font("IDAutomationHC39M", 14);
PointF point = new PointF(2f, 2f);
SolidBrush black = new SolidBrush(Color.Black);
SolidBrush White = new SolidBrush(Color.White);
graphics.FillRectangle(White, 0, 0, bitmap.Width, bitmap.Height);
graphics.DrawString ("*" + barcode + "*", oFont, black, point);
}
using (MemoryStream ms = new MemoryStream())
{
bitmap.Save(ms, ImageFormat.Png);
PictureBox1.Image = bitmap;
PictureBox1.Height = bitmap.Height;
PictureBox1.Width = bitmap.Width;
}
}
private void button5_Click(object sender, EventArgs e)
{
}
private void button5_Click_1(object sender, EventArgs e)
{
try
{
// panel1.Enabled = true;
App.Barcode.AddBarcodeRow(App.Barcode.NewBarcodeRow());
barcodeBindingSource.MoveLast();
BCode.Visible = true;
BCode.Enabled = true;
BCode.Focus();
BCode.Text = DateTime.Now.ToString("MMddyyyyHHmmss");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Mesaj", MessageBoxButtons.OK, MessageBoxIcon.Error);
App.Barcode.RejectChanges();
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
dataGridView1.DataSource = usersBindingSource;
dataGridView2.DataSource = barcodeBindingSource;
usersBindingSource.EndEdit();
App.Users.AcceptChanges();
barcodeBindingSource.EndEdit();
App.Barcode.AcceptChanges();
App.Users.WriteXml(string.Format("{0}//userdata.dat", Application.StartupPath));
App.Barcode.WriteXml(string.Format("{0}//userdata.dat", Application.StartupPath));
}
}
}
解决方案
你的问题不清楚。就像拼图一样。但是有一些可能的点:
两个不同的数据库使用相同的文件作为源是正确的吗?
String fileNameb = string.Format("{0}//userdata.dat", Application.StartupPath);
string fileName = string.Format("{0}//userdata.dat", Application.StartupPath);
我认为你的问题在这里。在关闭程序之前你写
App.Users.WriteXml(string.Format("{0}//userdata.dat", Application.StartupPath));
和
App.Barcode.WriteXml(string.Format("{0}//userdata.dat", Application.StartupPath));
我认为它会覆盖您的目标文件。
您可以使用两个不同的目标文件。
错误的:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
dataGridView1.DataSource = usersBindingSource;
dataGridView2.DataSource = barcodeBindingSource;
usersBindingSource.EndEdit();
App.Users.AcceptChanges();
barcodeBindingSource.EndEdit();
App.Barcode.AcceptChanges();
App.Users.WriteXml(string.Format("{0}//userdata.dat", Application.StartupPath));
App.Barcode.WriteXml(string.Format("{0}//userdata.dat", Application.StartupPath));
}
正确的:
....
String fileNameb = string.Format("{0}//barcodedata.dat", Application.StartupPath);
string fileName = string.Format("{0}//userdata.dat", Application.StartupPath);
....
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
dataGridView1.DataSource = usersBindingSource;
dataGridView2.DataSource = barcodeBindingSource;
usersBindingSource.EndEdit();
App.Users.AcceptChanges();
barcodeBindingSource.EndEdit();
App.Barcode.AcceptChanges();
App.Users.WriteXml(string.Format("{0}//userdata.dat", Application.StartupPath));
App.Barcode.WriteXml(string.Format("{0}//barcodedata.dat", Application.StartupPath));
}
推荐阅读
- java - 为什么我的 getter 和 setter 不能与 string.length() 一起使用?
- sql - 计算具有空值的最近连续行数
- jquery - 循环遍历表数据并使用 jQuery 获取数据
- scala - 从依赖类型问题的 HList 中的 Scala 无形选择
- powerbi - DAX 查询返回 1 个月前的文本值
- salesforce - 我正在学习 Apex 代码中的面向对象概念,并且不断收到 "Variable does not exist: name" 。我究竟做错了什么?
- c# - 如何在 Entity Framework Core 中设置多个一对一关系?
- scala - 假/真列常数
- c# - 将 DynamicExpression.ParseLambda 与字符串插值一起使用
- java - 从 txt.file 读取并使用 java 打印出它的 2d 数字