插入到Database.sdf文件 [英] Insert to Database.sdf file

查看:146
本文介绍了插入到Database.sdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi

我正在使用EF中的表对database.sdf文件进行一些基本的插入,所有连接都可以正常工作,但是在向数据库中插入,删除,更新值时遇到了问题.是吗?我在想什么?

插入代码

hi

I am doing some basic insert to database.sdf file using tables from EF,all connections work but am having problems inserting,deleting,updating values to the database. Y is that?What am i misng?

Code to insert

string strConnection = ConfigurationManager.ConnectionStrings["Database1Entities"].ConnectionString;
           using (var conn = new SqlCeConnection(string.Format("Data Source=Database1.sdf;", strConnection)))
           {
               conn.Open();

               try
               {
                   SqlCeCommand dbCmd = new SqlCeCommand();

String sqlAddNew = "INSERT INTO tbl_Credantials (password,username ) Values(@password,@username)";
               dbCmd = new SqlCeCommand(sqlAddNew, conn);


dbCmd.Parameters.Add("@password", SqlDbType.NVarChar).Value = txtuserID.Text;
dbCmd.Parameters.Add("@username", SqlDbType.NVarChar).Value = txtUsername.Text;

                   dbCmd.ExecuteNonQuery();
                                 }
               catch (SqlCeException)
               {
                   throw;
               }
               finally
               {
                   if (conn.State == ConnectionState.Open) conn.Close();
               }
           }




App.Config文件




App.Config file

<configuration>
  <connectionStrings>
    <add name="Database1Entities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlServerCe.3.5;provider connection string=&quot;Data Source=|DataDirectory|\Database1.sdf&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

推荐答案

检查ExecuteNonQuery是否确实进行了插入.该方法返回一个整数,该整数告诉您该命令影响了多少条记录.因此,在您的INSERT中,结果应为1(添加了一条记录)

您可以检查的另一件事是,您的连接字符串未指向一个SDF文件,当您运行该应用程序时,该SDF文件将被覆盖.例如,如果从bin/debug目录中使用了SDF,并且您已将SDF包含在项目中,则当您重新编译应用程序时,编译器可能会覆盖文件.这将导致您失去先前可能已经进行的所有更改.
Check that the ExecuteNonQuery actually makes the insert. The method returns an int which tells you how many records were affected by the command. So in your INSERT the results should be 1 (one record added)

Another thing you can check is that your connection string isn''t pointing to an SDF file which would be overwritten when you run the application. For example if the SDF is used from bin/debug directory and you have included the SDF in your project, the compiler may overwrite the file when you recompile the application. This would cause you to loose all changes that may have been done earlier.


首先确保您的连接字符串正确指向数据库.您使用了SqlDbType.NVarChar ...因此您需要确保文本框的文本长度限制.
取而代之的是,您可以使用SqlDbType.VarChar.
First ensure your connection string is right that points your database.You used SqlDbType.NVarChar...so you need to ensure the text length limit of your textboxes.
Instead of that you can use SqlDbType.VarChar.


这篇关于插入到Database.sdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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