如何将SQL数据库嵌入/附加到Visual C#中?

编程入门 行业动态 更新时间:2024-10-25 04:27:02
本文介绍了如何将SQL数据库嵌入/附加到Visual C#中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是我第一次使用SQL,这可能是一个愚蠢的问题,但是我进行了一些研究,但我认为我没有找到我想要的东西.

This is the first time I've used SQL and this is probably a stupid question but I've done some research and I don't think I'm finding what I'm looking for.

我想要的是一种制作私有SQL数据库的方法,该数据库将在我的C#程序中使用.我已经在SQL Server Express中建立了数据库,并将其连接到Visual Studio 2010.

What I want is a way to make a private SQL database that will be used within my C# program. I've already made a database within SQL Server Express and I've connected that to Visual Studio 2010.

SqlCommand DBAccess = new SqlCommand(); DBAccess.Connection = new SqlConnection( "Data Source = localhost;" + "Initial Catalog = My_Database;" + "Trusted_Connection = True");

  • 我可以将数据源嵌入程序中并将与解决方案的其余部分一起编译吗?
  • 该程序有一些额外的背景;

    Some extra background on the program;

    该程序需要搜索数据库中的6个表,并在搜索字符串与特定字段匹配时输出 DataRow 的内容.

    It's a program that needs to search through 6 tables within the database and output the contents of a DataRow when search string matches a particular field.

    EG.

    Field 1 Field 2 quick AA brown AA fox AB jumps AB over AA the AB lazy AB dog AA

    Search_String = AB

    Search_String = AB

    输出;

    fox jumps the lazy

    任何帮助将不胜感激!!!!!

    Any help will be much appreciated!!!!!

    谢谢

    推荐答案

    刚掌握(VS 2010):

    Just for getting a grip (VS 2010):

  • 创建控制台项目
  • 添加对System.Data.SqlServerCe的引用(在我的计算机上的Program Files \ Microsoft SQL Server Compact Edition \ v3.5 \ Desktop \ System.Data.SqlServerCe.dll中)
  • 在解决方案资源管理器中右键单击项目节点,选择添加=>新建项...",选择本地数据库",将其命名为MyDB
  • 新文件MyDB.sdf将添加到项目(MS SQL Server Compact数据库)中
  • 右键单击新文件,单击打开",数据库将在服务器资源管理器"中打开.
  • 在服务器资源管理器"中,展开MyDB.sdf,右键单击表",然后单击创建表"(将其命名为MyTable)
  • 添加两列"Field1"和"Field2"(暂时将其保留为nvarchar(100))
  • 右键单击新表,选择显示表数据",填写数据
  • 代码:

    using System.Data.SqlServerCe; namespace ConsoleApplication6 { class Program { static void Main(string[] args) { using (var cn = new SqlCeConnection("Data Source=MyDB.sdf")) { cn.Open(); using (var cmd = cn.CreateCommand()) { cmd.CommandText = "select * from MyTable where Field2 like '%AB%'"; using (var reader = cmd.ExecuteReader()) { while (reader.Read()) { Console.WriteLine("Field1: {0}", reader[0]); } } } } Console.ReadKey(); } } }

    输出的狐狸会让懒惰跳起来.

    Will output fox jumps the lazy.

    但,为简单起见,我会使用 SQlite .包装器在此处: system.data.sqlite/index.html/doc/trunk/www/index.wiki

    BUT, I would go with SQlite for simple purposes. Wrapper is here: system.data.sqlite/index.html/doc/trunk/www/index.wiki

更多推荐

如何将SQL数据库嵌入/附加到Visual C#中?

本文发布于:2023-11-10 01:07:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1573945.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何将   数据库   SQL   Visual

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!