我有一个名为NumLeter.mdf的本地Db两个表:LeterTb和NumberTb。我想插入NumberTb新值,当应用程序运行时,我插入的新值正确显示给我,好像它们已被插入,但是当关闭应用程序和显示表数据中的clic时,新数据未添加到表。我已经实现了EntityFramework v:6.1.3。这是我的代码:
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;
using System.Data.SqlClient;
namespace LearningVS
{
public partial class Form1 : Form
{
SqlConnection conexion;
public Form1()
{
InitializeComponent();
conexion = cConexion.getConexion();
}
private void btInsertNum_Click(object sender, EventArgs e)
{
insertNumber();
}
public void insertNumber()
{
SqlCommand command = new SqlCommand();
command.Connection = conexion;
command.CommandText = "INSERT INTO NumberTb(numberA, numberB, numberC) VALUES(@numberA, @numberB, @numberC)";
command.Parameters.Add("numberA", SqlDbType.Int, 3).Value = this.txNumberA.Text;
command.Parameters.Add("numberB", SqlDbType.Int, 3).Value = this.txNumberB.Text;
command.Parameters.Add("numberC", SqlDbType.Int, 3).Value = this.txNumberC.Text;
command.ExecuteNonQuery();
//testing this either
/*SqlDataAdapter datAdapt = new SqlDataAdapter(command);
NumLeterDataSet dtSet = new NumLeterDataSet();
datAdapt.FillSchema(dtSet, SchemaType.Source, "NumberTb");
datAdapt.Fill(dtSet, "NumberTb");
datAdapt.Update(dtSet.NumberTb);*/
}
//Also I tried by creating ADO.NET Entity Data Model whit the same result
private void button1_Click(object sender, EventArgs e)
{
using (var db = new NumDbModel())
{
var numbers = new NumberTb()
{
numberA = 22,
numberB = 33,
numberC = 44,
};
db.NumberTBs.Add(numbers);
db.SaveChanges();
}
}
}}
这是我处理连接的地方
namespace LearningVS
{
class cConexion
{
private static SqlConnection conexion;
public static SqlConnection getConexion()
{
if (conexion != null)
{
return conexion;
}
conexion = new SqlConnection(Properties.Settings.Default.NumLetersConnectionString);
try
{
conexion.Open();
return conexion;
}
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show("Error de conexión" + e.Message);
return null;
}
}
public static void cerrarConexion()
{
if (conexion != null)
{
conexion.Close();
}
}
}}