C# 我在一个数据集中有两个数据表。我通常登录和注销。当我返回时,其中一个数据表是空的
我将用户信息存储在“Users”表中。 我将这些用户生成的条形码信息保存在“条形码”表中。 每次“用户”内容都是空的。我找不到解决办法。 删除其中一个数据表时,另一个数据表正常工作。 创建单独的数据集更准确C# 我在一个数据集中有两个数据表。我通常登录和注销。当我返回时,其中一个数据表是空的,c#,datatable,dataset,C#,Datatable,Dataset,我将用户信息存储在“Users”表中。 我将这些用户生成的条形码信息保存在“条形码”表中。 每次“用户”内容都是空的。我找不到解决办法。 删除其中一个数据表时,另一个数据表正常工作。 创建单独的数据集更准确 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using Sys
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));
}
}
}
你的问题不清楚。就像拼图一样。但也有一些可能的观点: 两个不同的数据库使用同一个文件作为源,是否正确
stringfilenameb=String.Format(“{0}//userdata.dat”,Application.StartupPath);
字符串文件名=string.Format(“{0}//userdata.dat”,Application.StartupPath);
我想这是你的问题。
在关闭您编写的程序之前
WriteXml(string.Format(“{0}//userdata.dat”,Application.StartupPath))
及
App.Barcode.WriteXml(string.Format(“{0}//userdata.dat”,Application.StartupPath))
我想它会覆盖你的目标文件
您可以使用两个不同的目标文件
错:
private void Form1\u FormClosing(对象发送方,FormClosingEventArgs e)
{
dataGridView1.DataSource=usersBindingSource;
dataGridView2.DataSource=barcodeBindingSource;
usersBindingSource.EndEdit();
App.Users.AcceptChanges();
barcodeBindingSource.EndEdit();
App.Barcode.AcceptChanges();
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.Format(“{0}//userdata.dat”,Application.StartupPath);
....
私有作废Form1\u FormClosing(对象发送方,FormClosingEventArgs e)
{
dataGridView1.DataSource=usersBindingSource;
dataGridView2.DataSource=barcodeBindingSource;
usersBindingSource.EndEdit();
App.Users.AcceptChanges();
barcodeBindingSource.EndEdit();
App.Barcode.AcceptChanges();
WriteXml(string.Format(“{0}//userdata.dat”,Application.StartupPath));
App.Barcode.WriteXml(string.Format(“{0}//barcodedata.dat”,Application.StartupPath));
}
欢迎使用堆栈溢出!不清楚你在问什么。请复习并帮助你提出一个好问题,从而得到一个好答案