我正在学习C#和SQL交互的基础知识,我有这样的形式:
它显示了使用VS 2013 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复制到&#34; / bin / Debug&#34;文件夹,我添加了一条新记录,然后从服务器资源管理器中分离出DB,并从&#34; / bin / Debug&#34;复制了DB.mdf;文件夹并替换主项目文件夹中的DB.mdf。它确认这可能是问题,因为现在我可以通过VS DB Designer看到新添加的记录。
现在我只需要弄清楚如何在&#34; / bin / Debug&#34;更新对DB.mdf所做的更改。 VS关闭时主文件DB.mdf的文件夹。
答案 0 :(得分:4)
请注意,运行程序会将数据库从根项目文件夹复制到Release或Debug文件夹,因此每次运行程序时,您都将使用数据库的新副本。这意味着下次运行应用程序时,对您所创建的数据库的任何修改都将被覆盖并丢弃。当您刚开发应用程序时,这很好。如果您不想要此行为,请在解决方案资源管理器中选择数据库文件(.mdf),然后在“属性”窗口中找到“复制到输出目录”选项。如果更新,则将其值更改为“复制”。项目根目录中的数据库文件现在只有在输出目录中已存在的较新版本时才会被删除。
显然,当您首次创建解决方案/项目数据库时,它将在项目的父目录中创建。 之后,当您构建/调试解决方案时,它会在Bin / Debug目录中添加数据库。调试时对数据所做的任何更改都在Debug数据库中进行。 但是,每次在VS中运行应用程序时,它都会从父目录中提取数据,而父目录从未收到调试时所做的更改,因为实际上是在调试数据库中进行了更改。
<强>解决方案:强>