如何将数据插入Sql Compact Server? [英] How Do I Insert Data In To Sql Compact Server?

查看:69
本文介绍了如何将数据插入Sql Compact Server?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为sql compact服务器开发了插入数据的代码?



  private   void  btnOK_Click( object  sender,EventArgs e)
{
string connetionString = null ;

connetionString = Data Source = stockDb.sdf; Persist Security Info = False;


使用(SqlCeConnection con = new SqlCeConnection(connetionString) )
{

con.Open();
SqlConnection。


string sqlquery =( 插入查询(userName,cusName,cusContact,moreDetails,tktType,noPax,firstStatus,secStatus,thirdStatus)值(@ uName,@ cName,@ cContact,@ mDetails,@ tType,@ nPax,@ fStatus, @ sStatus,@ tStatus));

SqlCeCommand command = new SqlCeCommand(sqlquery,con);



int value = Convert.ToInt32 (Cus_PaxNo.Text);

command.Parameters.AddWithValue( @ uName,Loginlbl.Text );
command.Parameters.AddWithValue( @ cName,Cus_Name.Text);
command.Parameters.AddWithValue( @ cContact,Cus_Contact.Text);
command.Parameters.AddWithValue( @ mDetails,Cus_Details.Text);
command.Parameters.AddWithValue( @ tType,Cus_TicketType.Text);
command.Parameters.AddWithValue( @ nPax);
command.Parameters.AddWithValue( @ fStatus,Cus_FirstStatus.Text);
command.Parameters.AddWithValue( @ sStatus,Cus_SecondStatus.Text);
command.Parameters.AddWithValue( @ tStatus,Cus_ThirdStatus.Text);
command.ExecuteNonQuery();

MessageBox.Show( 密码接受);

}





command.ExecuteNonQuery();

此代码给了我错误说。





System.Data.SqlserverCe中未处理的类型'System.Data.SqlServerCe.SqlCeException'的异常处理。 Dll



我能做些什么来解决这个问题。

解决方案

首先找出什么报告的问题是:

添加一个 try ... catch 阻止该代码(使用using块就可以了)并检查异常在调试器中捕获的对象:它将包含有关问题的详细信息。没有它,我们都只是在黑暗中吹口哨。



很有可能它很简单,比如你所分配的列太大了在数据库中,或者一个不唯一的唯一值(例如主键)。



但是直到你收到消息,并且可以准确地看一下你输入参数的数据,没有人能说出来。

我们无法为你做到这一点:我们无法访问你的数据库或你正在输入的数据。 />


BTW:消息框暗示您以明文形式存储密码:如果是,那就是 Code Crime [ ^ ]并且非常非常糟糕理念。请参阅此处:密码存储:如何操作。 [ ^ ]

i have developed a code for insert data to sql compact server?

private void btnOK_Click(object sender, EventArgs e)
        {
            string connetionString = null;

            connetionString = "Data Source=stockDb.sdf;Persist Security Info=False";


            using (SqlCeConnection con = new SqlCeConnection(connetionString))
            {
        
                con.Open();
                SqlConnection.
            

                string sqlquery = ("insert into Enquiry(userName,cusName,cusContact,moreDetails,tktType,noPax,firstStatus,secStatus,thirdStatus)Values(@uName,@cName,@cContact,@mDetails,@tType,@nPax,@fStatus,@sStatus,@tStatus)");

                SqlCeCommand command = new SqlCeCommand(sqlquery, con);



                    int value = Convert.ToInt32(Cus_PaxNo.Text);

                    command.Parameters.AddWithValue("@uName", Loginlbl.Text);
                    command.Parameters.AddWithValue("@cName", Cus_Name.Text);
                    command.Parameters.AddWithValue("@cContact", Cus_Contact.Text);
                    command.Parameters.AddWithValue("@mDetails", Cus_Details.Text);
                    command.Parameters.AddWithValue("@tType", Cus_TicketType.Text);
                    command.Parameters.AddWithValue("@nPax", value);
                    command.Parameters.AddWithValue("@fStatus", Cus_FirstStatus.Text);
                    command.Parameters.AddWithValue("@sStatus", Cus_SecondStatus.Text);
                    command.Parameters.AddWithValue("@tStatus", Cus_ThirdStatus.Text);
                    command.ExecuteNonQuery();

                        MessageBox.Show("Password Accepted");

                }



command.ExecuteNonQuery();
this code gives me an error saying.


An unhandled exception of type 'System.Data.SqlServerCe.SqlCeException'occured in System.Data.SqlserverCe.Dll

What i can do for this to solve the issue.

解决方案

Start by finding out what the reported problem is:
Add a try...catch block round that code (with the using block will be fine) and examine the Exception object that is caught in the debugger: it will contain more detail on exactly what the problem is. Without that, we are all just whistling in the dark.

The chances are that it's something pretty simple, like a field that is too big for the column you have allocated in the DB, or a unique value (such as a primary key) that isn't unique.

But until you get the message, and can look at exactly what data you are feeding into the parameters, nobody can tell.
And we can't do it for you: we don't have access to your DB, or the data you are feeding in.

BTW: the message box implies that you are storing passwords in clear text: if so, that is a Code Crime[^] and is a very, very bad idea. See here: Password Storage: How to do it.[^]


这篇关于如何将数据插入Sql Compact Server?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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