Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SQL更改在VS重新启动后消失_C#_Sql Server - Fatal编程技术网

C# SQL更改在VS重新启动后消失

C# SQL更改在VS重新启动后消失,c#,sql-server,C#,Sql Server,我正在学习C#和SQL交互的基础知识,我有以下表格: 它向我显示了使用VS 2013 DB builder制作的预填充DB的值,并且在上面的按钮上一切正常 这是我的桌子: 问题: 当我单击AddNew时,文本框被清除 接下来,填充文本框,当我单击Save时,记录显然被添加到DB(或者至少添加到DataSet?),因为它会给我一条成功消息,并在我使用上面的按钮浏览DB时显示 如果我关闭并重新打开我的应用程序(不关闭VS),我插入的记录仍然存在 但是如果我关闭并重新打开VS并再次运行我的应用程

我正在学习C#和SQL交互的基础知识,我有以下表格:

它向我显示了使用VS 2013 DB builder制作的预填充DB的值,并且在上面的按钮上一切正常

这是我的桌子:

问题:

  • 当我单击AddNew时,文本框被清除
  • 接下来,填充文本框,当我单击Save时,记录显然被添加到DB(或者至少添加到DataSet?),因为它会给我一条成功消息,并在我使用上面的按钮浏览DB时显示
  • 如果我关闭并重新打开我的应用程序(不关闭VS),我插入的记录仍然存在
  • 但是如果我关闭并重新打开VS并再次运行我的应用程序,我输入的记录将消失
  • 另外,在不关闭VS的情况下,如果我转到服务器资源管理器(VS左窗格),右键单击my table=>Show table Data,则新记录永远不会出现
以下是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;
    }

}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
名称空间SQLtest
{
/// 
///保持我们的状态的班级
/// 
公共部分类Form1:Form
{
//变数
数据库连接对象连接;
串构造;
数据集ds;
数据行dr;
int最大行;
int inc=0;
/// 
///类的构造函数
/// 
公共表格1()
{
初始化组件();
}
/// 
///表单初始化方法
/// 
/// 
/// 
私有void Form1\u加载(对象发送方、事件参数e)
{
尝试
{
objConnect=new DBconnection();
consting=Properties.Settings.Default.TestConnectionString;
objConnect.connection_string=构造;
objConnect.Sql=Properties.Settings.Default.Sql;
ds=objConnect.GetConnection;
maxRows=ds.Tables[0].Rows.Count;
//MessageBox.Show(“最大行数:”+maxRows);
导航记录();
}
捕获(异常错误)
{
MessageBox.Show(错误消息);
}
}
//数据库导航方法
/// 
///将指针(inc)设置为最后一行
/// 
/// 
/// 
private void btnNext_单击(对象发送方,事件参数e)
{
//防止索引自动边界错误
如果(inc!=maxRows-1)
{
inc++;
导航记录();
}
其他的
{
Show(“到达最后一名员工。显示第一条记录”);
inc=0;
导航记录();
}
}
/// 
///将指针(inc)设置为上一行(如果可能)
/// 
/// 
/// 
private void btnPrevious_Click(对象发送者,事件参数e)
{
//防止索引自动边界错误
如果(inc!=0)
{
公司--;
导航记录();
}
其他的
{
Show(“到达第一个员工。显示最后一条记录”);
inc=最大行数-1;
导航记录();
}
}
/// 
///将指针(inc)设置为第一行
/// 
/// 
/// 
私有void btnFirst_单击(对象发送方,事件参数e)
{
如果(inc!=0)
{
inc=0;
导航记录();
}
其他的
{
MessageBox.Show(“已在第一个员工身上”);
}
}
/// 
///将指针(inc)设置为最后一行
/// 
/// 
/// 
私有无效btnLast_单击(对象发送方,事件参数e)
{
如果(inc!=maxRows-1)
{
inc=最大行数-1;
导航记录();
}
其他的
{
MessageBox.Show(“已在上一个员工身上”);
}
}
/// 
///主导航方法
/// 
专用void导航记录()
{
dr=ds.表[0].行[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();
}
/// 
///退出按钮处理程序
/// 
/// 
/// 
私有void btnExit\u单击(对象发送者,事件参数e)
{
Application.Exit();
}
/// 
///添加新记录按钮。只需清除文本字段,即可添加新记录。
/// 
/// 
/// 
私有无效btnAddNew_单击(对象发送者,事件参数e)
{
txtFirstName.Clear();
txtLastName.Clear();
txtJobTitle.Clear();
txtDepartment.Clear();
btnAddNew.Enabled=false;
btnSave.Enabled=true;
btnCancel.Enabled=真;
}
/// 
///保存新记录按钮
/// 
/// 
/// 
私有void btnSave\u单击(对象发送方,事件参数e)
{
DataRow newRow=ds.Tables[0].newRow();
//newRow[0]是id,因此它会自动填充
newRow[1]=txtFirstName.Text;
newRow[2]=txtLastName.Text;
newRow[3]=txtJobTitle.Text;
newRow[4]=txtDepartment.Text;
ds.Tables[0].Rows.Add(newRow);
尝试
{
objConnect.UpdateDB(ds);
maxRows++;
inc=最大行数-1;
MessageBox.Show(“数据库更新成功”);
}
捕获(异常错误)
{
MessageBox.Show(错误消息);
}
btnAddNew.Enabled=true;
btnSave.Enabled=false;
btnCancel.Enabled=false;
}
/// 
///取消新记录按钮。只需调用NavigateRecords()方法并还原按钮。
/// 
/// 
/// 
私有void btnCancel\u单击(对象发送者,事件参数e)
{
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]);
    }
}
}