在.sdf生活中插入数据的问题 [英] Problem to insert data in .sdf life

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

问题描述

尊敬的专家,

我是C#的新手.我的要求是动态创建数据库文件(在本地系统中),做类似设置和操作的操作得到.我能够动态创建.sdf文件,并且能够成功读取.但是在将数据插入.sdf文件时,其无法正常工作.

我创建.sdf文件的方式是
通过sloution Explorer添加-> NewItem-> Data-> LocalDatabase.

下面,我更新了我的代码(创建了4个基于控制台的应用程序以进行测试).请让我知道代码中的错误.



Dear Expert,

I''m new to C#. My requirement is to create a DB file (in local system) dynamically & do operation like set & get. I''m able to create .sdf file dynamically and successfully able to read. but while inserting data in .sdf file, its not working properly.

The way i created .sdf file is
Add->NewItem->Data->LocalDatabase through sloution explorer.

Below I''ve updated my code (created 4 console based app for testing purpose). Please let me know what is the wrong in the code.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlServerCe;
using System.Data.SqlClient;

namespace ConsoleApplication2
{
    class Program
    {
        static public void ReadData()
        {
            string connStr = "Data Source = TempDB.sdf; Password =";

            //if (!File.Exists("DBName.sdf"))
            {
                SqlCeConnection conn = new SqlCeConnection(connStr);
                try
                {
                    SqlCeEngine engine = new SqlCeEngine(connStr);
                   
                    conn.Open();

                    SqlCeCommand cmd = conn.CreateCommand();
                    
                    cmd.CommandText = "SELECT * FROM TestTable WHERE Name = 'Krishna'";
                    SqlCeDataReader myReader = cmd.ExecuteReader();

                    while (myReader.Read())
                    {
                        string strGroup = myReader.GetString(1);
                        
                        int ll = 0;
                        ll++;
                    }

                    

                }
                catch (SqlException ex)
                //catch(Exception)
                {
                    string strMessage = "\nFailed to Read ***";
                    strMessage += ex.Message;
                    // Log the exception
                    Console.WriteLine(strMessage);
                }
                finally
                {
                    conn.Close();
                }
            } 
        }

        static public void WriteData()
        {
            string connStr = "Data Source = TempDB.sdf;";

            //if (!File.Exists("DBName.sdf"))
            {
                SqlCeConnection conn = new SqlCeConnection(connStr);
                conn.Open();
                SqlCeCommand cmd = new SqlCeCommand("INSERT INTO TestTable ([Name], [Group]) Values('Krishna', 'D1')", conn);
                try
                {
                    int iRes = cmd.ExecuteNonQuery();
                    int t = 0;
                    t++;
                }
                catch (Exception)
                {
                    Console.WriteLine("\nFailed to Write\n");
                }
                conn.Close();
            }
        }

        static void Main(string[] args)
        {
            // Write data
            WriteData();

            // Read Data
            ReadData();
        }
    }
}




提前谢谢大家.




Thank you all in advance.

推荐答案

很可能Connection.OPen引发异常,可能是因为它找不到您的数据库.尝试指定路径,而不要依赖当前文件夹.

顺便说一句:您应该处置SqlCeCommand和SqlCeConnection对象,因为它们是稀缺资源,除非您在应用程序的生命周期内保持Connection处于打开状态(这对于SqlCe数据库是可以的,但即使那样也会导致备份软件出现问题.)
The chances are that the Connection.OPen is throwing an exception, probably because it can''t find your DB. Try specifying the path instead of relying on the current folder.

BTW: You should Dispose SqlCeCommand and SqlCeConnection objects as they are scarce resources, unles you keep the Connection open for the life of the application (which can be ok with a SqlCe database, but even then can cause problems with backup software.)


这篇关于在.sdf生活中插入数据的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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