问题插入C#中的SQL Server压缩数据库 [英] problem insert to SQL server compact database in C#

查看:129
本文介绍了问题插入C#中的SQL Server压缩数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将数据插入我的SQL服务器压缩数据库(.sdf)中的C#

这是我的代码i allways popup not inserted错误消息

waht是wronge!

我是新来的C#

提前感谢!









  int  index = cb1.SelectedIndex;  //   cb1 =组合框 
SqlCeConnection con = new SqlCeConnection( Data Source = C:\ application \ application。 sdf );
con.Open();
SqlCeCommand cm = new SqlCeCommand( INSERT INTO tbl_user(u_name,u_scl,u_cmb,u_id)VALUES(@ u_name,@ u_scl,@ u_cmb,@ u_id),con);
cm.Parameters.AddWithValue( @ u_name,txtname.Text);
cm.Parameters.AddWithValue( @ u_scl,txtscl.T.Text);
cm.Parameters.AddWithValue( @ u_cmb,index);
cm.Parameters.AddWithValue( @ u_id 1 );
尝试
{
int eff = cm.ExecuteNonQuery() ;

if (eff == 0
{
MessageBox.Show( 学生已添加 完成);
}
else
{
MessageBox.Show( 未插入 错误);
}
}
catch (SqlCeException ex)
{
MessageBox.Show(ex.ToString( ));
// MessageBox.Show(用户未插入,错误);
}
con.Close();

解决方案

与数据库交叉检查该行是否实际插入,因为ExecuteNonQuery()

方法将返回受影响的行数,因此如果正确插入一行则应为1,如果eff不是0,则显示消息'not inserted'。

设置断点int eff = cm.ExecuteNonQuery(); 





找到eff的值相应地修改代码。

同时验证你的连接字符串。

数据源= C:\ application \ application.sdf;持久安全信息=错误;



这是sql server compact的标准连接字符串。



-

谢谢


I trying to insert data to my SQL server compact database(.sdf) in C#
This is my code i allways popup not inserted error message
waht is the wronge !
I new to C#
Thank in advance!




int index= cb1.SelectedIndex; //cb1=combo box
            SqlCeConnection con = new SqlCeConnection("Data Source="C:\application\application.sdf"");
            con.Open();
            SqlCeCommand cm = new SqlCeCommand("INSERT INTO tbl_user(u_name, u_scl, u_cmb, u_id) VALUES(@u_name, @u_scl, @u_cmb, @u_id)", con);
            cm.Parameters.AddWithValue("@u_name", txtname.Text);
            cm.Parameters.AddWithValue("@u_scl", txtscl.T.Text);
            cm.Parameters.AddWithValue("@u_cmb",index);
            cm.Parameters.AddWithValue("@u_id", 1);
            try
            {
                int eff = cm.ExecuteNonQuery();
                
                if (eff == 0)
                {
                    MessageBox.Show("Student Added ", "Done");
                }
                else
                {
                    MessageBox.Show("not inserted ", "Error");
                }
            }
            catch (SqlCeException ex)
            {
                MessageBox.Show(ex.ToString());
                //MessageBox.Show("User not Inserted", "Error");
            }
            con.Close();

解决方案

cross check with database that row is actually inserted or not because ExecuteNonQuery()
method will return number of rows affected so it should be 1 if one row is inserted properly and you are displaying message 'not inserted' if eff is other than 0.
set breakpoint at

int eff = cm.ExecuteNonQuery();



find the value of eff and modify code accordingly.
Also verify your connection string.
Data Source=C:\application\application.sdf;Persist Security Info=False;

this is standard connection string for sql server compact.

--
Thanks


这篇关于问题插入C#中的SQL Server压缩数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆