not save my the data
-
I'm working on dataGrid Save content based on SQL Server data, C # smart devices and I have all the code I do not make mistake but not save data
/////////////////inicio guardar
//cadena de conexion
string partDir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
string partBD = System.IO.Path.Combine(partDir, "embarque.sdf");
string connectionstring = string.Format(@"DataSource={0}; Password ='root'", partBD);try
{
using (SqlCeConnection con = new SqlCeConnection(connectionstring))
{
string ConsultaSQL = "INSERT INTO ENVIO VALUES(@RAID, @NPARTE, @CANTIDAD, @VENTANA, @LIDER, @FECHA)";foreach (DataRow row in dt.Rows) { SqlCeCommand cmd = new SqlCeCommand(ConsultaSQL, con); cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("@RAID", Convert.ToString(row\["Raid"\].ToString())); cmd.Parameters.AddWithValue("@NPARTE", Convert.ToString(row\["Nparte"\].ToString())); cmd.Parameters.AddWithValue("@CANTIDAD", Convert.ToString(row\["Cantidad"\].ToString())); cmd.Parameters.AddWithValue("@VENTANA", Convert.ToString(row\["Ventana"\].ToString())); cmd.Parameters.AddWithValue("@LIDER", Convert.ToString(row\["Lider"\].ToString())); cmd.Parameters.AddWithValue("@FECHA", Convert.ToString(row\["Fecha"\].ToString())); con.Open(); cmd.ExecuteNonQuery(); con.Close(); } } MessageBox.Show("Datos Agregados");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
///////////////fin guardado -
I'm working on dataGrid Save content based on SQL Server data, C # smart devices and I have all the code I do not make mistake but not save data
/////////////////inicio guardar
//cadena de conexion
string partDir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
string partBD = System.IO.Path.Combine(partDir, "embarque.sdf");
string connectionstring = string.Format(@"DataSource={0}; Password ='root'", partBD);try
{
using (SqlCeConnection con = new SqlCeConnection(connectionstring))
{
string ConsultaSQL = "INSERT INTO ENVIO VALUES(@RAID, @NPARTE, @CANTIDAD, @VENTANA, @LIDER, @FECHA)";foreach (DataRow row in dt.Rows) { SqlCeCommand cmd = new SqlCeCommand(ConsultaSQL, con); cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("@RAID", Convert.ToString(row\["Raid"\].ToString())); cmd.Parameters.AddWithValue("@NPARTE", Convert.ToString(row\["Nparte"\].ToString())); cmd.Parameters.AddWithValue("@CANTIDAD", Convert.ToString(row\["Cantidad"\].ToString())); cmd.Parameters.AddWithValue("@VENTANA", Convert.ToString(row\["Ventana"\].ToString())); cmd.Parameters.AddWithValue("@LIDER", Convert.ToString(row\["Lider"\].ToString())); cmd.Parameters.AddWithValue("@FECHA", Convert.ToString(row\["Fecha"\].ToString())); con.Open(); cmd.ExecuteNonQuery(); con.Close(); } } MessageBox.Show("Datos Agregados");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
///////////////fin guardadoHow do you know it's not saving the data? I see that you are using the executable path for the location of the database, so I assume that you have it in a root folder of your project. So, when you build, the file is (in all probability) copied over the one in the executable location. You have two possible fixes: 1. Change the file property in Visual Studio so that it only copies when newer. 2. Don't use the executable location. Option 2 is the better choice because you won't be able to update the data in the database once you install it into a protected folder such as inside Program Files. You should look to use AppData to store your file instead.
This space for rent
-
I'm working on dataGrid Save content based on SQL Server data, C # smart devices and I have all the code I do not make mistake but not save data
/////////////////inicio guardar
//cadena de conexion
string partDir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
string partBD = System.IO.Path.Combine(partDir, "embarque.sdf");
string connectionstring = string.Format(@"DataSource={0}; Password ='root'", partBD);try
{
using (SqlCeConnection con = new SqlCeConnection(connectionstring))
{
string ConsultaSQL = "INSERT INTO ENVIO VALUES(@RAID, @NPARTE, @CANTIDAD, @VENTANA, @LIDER, @FECHA)";foreach (DataRow row in dt.Rows) { SqlCeCommand cmd = new SqlCeCommand(ConsultaSQL, con); cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("@RAID", Convert.ToString(row\["Raid"\].ToString())); cmd.Parameters.AddWithValue("@NPARTE", Convert.ToString(row\["Nparte"\].ToString())); cmd.Parameters.AddWithValue("@CANTIDAD", Convert.ToString(row\["Cantidad"\].ToString())); cmd.Parameters.AddWithValue("@VENTANA", Convert.ToString(row\["Ventana"\].ToString())); cmd.Parameters.AddWithValue("@LIDER", Convert.ToString(row\["Lider"\].ToString())); cmd.Parameters.AddWithValue("@FECHA", Convert.ToString(row\["Fecha"\].ToString())); con.Open(); cmd.ExecuteNonQuery(); con.Close(); } } MessageBox.Show("Datos Agregados");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
///////////////fin guardadoAs Pete says, the database location is probably relevant here - you should never store data in the app folder as it tends to work in development, but fail in production. The reason for that is that in production it's installed under "Program Files" (or its x86 equivelant) which is protected from changes by the system to help prevent virus activity. Have a look here: Where should I store my data?[^] - it shows some better places, and how to access them. In addition, it's a poor idea to just us an anonymous INSERT - you should list the columns in the order you are going to INSERT them as this means your code is more resilient to changes in your DB. Use:
INSERT INTO ENVIO (Raid, Nparte, Cantidad, Ventana, Lider, Fecha) VALUES (@RAID, @NPARTE, @CANTIDAD, @VENTANA, @LIDER, @FECHA)
(Or similar) instead.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...