我正在学习C#和SQL交互的基础知识,并且具有以下形式:
它向我显示了使用VS 2013 DB Builder构建的预填充DB的值,上方的按钮一切正常。
这是我的桌子:
问题:
这是Form1.cs代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SQLtest
{
/// <summary>
/// A class that constains our form
/// </summary>
public partial class Form1 : Form
{
// variables
DBconnection objConnect;
string conString;
DataSet ds;
DataRow dr;
int maxRows;
int inc = 0;
/// <summary>
/// Constructor of the class
/// </summary>
public Form1()
{
InitializeComponent();
}
/// <summary>
/// Form initialization method
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, EventArgs e)
{
try
{
objConnect = new DBconnection();
conString = Properties.Settings.Default.TestConnectionString;
objConnect.connection_string = conString;
objConnect.Sql = Properties.Settings.Default.SQL;
ds = objConnect.GetConnection;
maxRows = ds.Tables[0].Rows.Count;
// MessageBox.Show("Max Rows: " + maxRows);
NavigateRecords();
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
}
// DB Navigation Methods
/// <summary>
/// Sets pointer (inc) to last row
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnNext_Click(object sender, EventArgs e)
{
// prevent indexOutOfBounds error
if (inc != maxRows - 1)
{
inc++;
NavigateRecords();
}
else
{
MessageBox.Show("Reached last employee. Showing the first record.");
inc = 0;
NavigateRecords();
}
}
/// <summary>
/// Sets pointer (inc) to previous row (if possible)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnPrevious_Click(object sender, EventArgs e)
{
// prevent indexOutOfBounds error
if (inc != 0)
{
inc--;
NavigateRecords();
}
else
{
MessageBox.Show("Reached first employee. Showing the last record.");
inc = maxRows - 1;
NavigateRecords();
}
}
/// <summary>
/// Sets pointer (inc) to first row
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnFirst_Click(object sender, EventArgs e)
{
if (inc != 0)
{
inc = 0;
NavigateRecords();
}
else
{
MessageBox.Show("Already on first employee.");
}
}
/// <summary>
/// Sets pointer (inc) to last row
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnLast_Click(object sender, EventArgs e)
{
if (inc != maxRows - 1)
{
inc = maxRows - 1;
NavigateRecords();
}
else
{
MessageBox.Show("Already on last employee.");
}
}
/// <summary>
/// Main Navigation Method
/// </summary>
private void NavigateRecords()
{
dr = ds.Tables[0].Rows[inc];
txtFirstName.Text = dr.ItemArray.GetValue(1).ToString();
txtLastName.Text = dr.ItemArray.GetValue(2).ToString();
txtJobTitle.Text = dr.ItemArray.GetValue(3).ToString();
txtDepartment.Text = dr.ItemArray.GetValue(4).ToString();
}
/// <summary>
/// Exit button handler
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
/// <summary>
/// Add new record Button. Simply clears text fields, ready for a new record to be added.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnAddNew_Click(object sender, EventArgs e)
{
txtFirstName.Clear();
txtLastName.Clear();
txtJobTitle.Clear();
txtDepartment.Clear();
btnAddNew.Enabled = false;
btnSave.Enabled = true;
btnCancel.Enabled = true;
}
/// <summary>
/// Save a new record button
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSave_Click(object sender, EventArgs e)
{
DataRow newRow = ds.Tables[0].NewRow();
// newRow[0] is the id, thus its filled automatically
newRow[1] = txtFirstName.Text;
newRow[2] = txtLastName.Text;
newRow[3] = txtJobTitle.Text;
newRow[4] = txtDepartment.Text;
ds.Tables[0].Rows.Add(newRow);
try
{
objConnect.UpdateDB(ds);
maxRows++;
inc = maxRows - 1;
MessageBox.Show("DB updated successfully");
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
btnAddNew.Enabled = true;
btnSave.Enabled = false;
btnCancel.Enabled = false;
}
/// <summary>
/// Cancel new record button. Simply call NavigateRecords() method, and restores buttons.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnCancel_Click(object sender, EventArgs e)
{
NavigateRecords();
btnAddNew.Enabled = true;
btnSave.Enabled = false;
btnCancel.Enabled = false;
}
}
}
这是DBconnection.cs代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SQLtest
{
/// <summary>
/// A class that makes the connection to the SQL Database
/// </summary>
class DBconnection
{
// variables
private string sql_string;
private string strCon;
System.Data.SqlClient.SqlDataAdapter da_1;
// set methods
public string Sql
{
set { sql_string = value; }
}
public string connection_string
{
set { strCon = value; }
}
// DataSet
public System.Data.DataSet GetConnection
{
get { return MyDataSet(); }
}
// MyDataSet method
private System.Data.DataSet MyDataSet()
{
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(strCon);
con.Open();
da_1 = new System.Data.SqlClient.SqlDataAdapter(sql_string, con);
System.Data.DataSet dat_set = new System.Data.DataSet();
da_1.Fill(dat_set,"Table_Data_1");
con.Close();
return dat_set;
}
// Update DB method
public void UpdateDB(System.Data.DataSet ds)
{
System.Data.SqlClient.SqlCommandBuilder cb = new System.Data.SqlClient.SqlCommandBuilder(da_1);
cb.DataAdapter.Update(ds.Tables[0]);
}
}
}
编辑:
为了测试VS将DB.mdf复制到“ / bin / Debug”文件夹中,我添加了一条新记录,然后从服务器资源管理器中分离了DB,然后从“ / bin / Debug”文件夹中复制了DB.mdf并替换了主目录上的DB.mdf。项目文件夹。它证实了这可能是问题所在,因为现在我可以通过VS DB Designer看到新添加的记录。
现在,我只需要弄清楚如何在VS关闭时将对“ / bin / Debug”文件夹中的DB.mdf所做的更改更新到主项目DB.mdf。
请注意,运行程序会将数据库从根项目文件夹复制到Release或Debug文件夹,因此,每次运行程序时,都将使用数据库的新副本。这意味着您对数据库所做的任何修改将在下次运行应用程序时被覆盖并被丢弃。当您只是在开发应用程序时,这很好。如果您不希望出现这种情况,请在“解决方案资源管理器”中选择数据库文件(.mdf),然后在“属性”窗口中找到“复制到输出目录”选项。将其值更改为Copy(如果较新)。现在,仅当项目的根目录中的数据库文件是输出目录中已有的较新版本时,才将其复制。
显然,当您第一次创建解决方案/项目数据库时,它是在项目的父目录中创建的。之后,当您构建/调试解决方案时,它将数据库添加到Bin / Debug目录中。调试时对数据所做的任何更改都在Debug数据库中进行。但是,每次在VS中运行应用程序时,它都会从父目录中提取数据,而该父目录从未收到您在调试时所做的更改,因为这些更改实际上是在Debug数据库中进行的。
解决方案:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句