c#连接mdf文件示例分享

时间:2022-12-15 10:48:33 

C#连接本地.mdf文件:项目中右键点击,新增——数据——基于服务的数据库,项目下直接生成.mdf数据库文件,后台(数据库的写入用参数传递):


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

namespace ADO.NET
{
    class Program
    {
        static void Main(string[] args)
        {
            //SQLServer 附加mdf文件
            string dataDir = AppDomain.CurrentDomain.BaseDirectory;
            if (dataDir.EndsWith(@"\bin\Debug\") || dataDir.EndsWith(@"\bin\Release\"))
            {
                dataDir = System.IO.Directory.GetParent(dataDir).Parent.Parent.FullName;
                AppDomain.CurrentDomain.SetData("DataDirectory", dataDir);
            }
            using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"))
            {
                conn.Open();
                //写入一条数据
                string strUserName = "作业本";
                string strPWD = "Ab123456";

                using (SqlCommand sqlCmd = conn.CreateCommand())
                {
                    sqlCmd.CommandText = "insert into Mytable1(Name,Password) values (@UserName,@PWD) ";//连接字符串进行参数化
                    SqlParameter[] sqlPara = new SqlParameter[] {
                    new SqlParameter("UserName",strUserName),
                    new SqlParameter("PWD",strPWD)
                    };
                    sqlCmd.Parameters.AddRange(sqlPara); //把Paramerter 数组参数添加到sqlCmd中
                    sqlCmd.ExecuteNonQuery();
                    Console.WriteLine("Insert OK");
                }

                //从表中读取数据
                string strRead = "SELECT   ID, Name, Password FROM      MyTable1 ";
                using (SqlCommand sqlCmd = new SqlCommand(strRead, conn))
                {
                    //sqlDataReader 逐行读取数据
                    using (SqlDataReader sdr = sqlCmd.ExecuteReader())
                    {
                        while (sdr.Read())
                        {

                            int id = sdr.GetInt32(sdr.GetOrdinal("ID"));  //sdr.GetOrdinal 获取列的序号
                            string Name = sdr.GetString(sdr.GetOrdinal("Name"));
                            bool PWD = sdr.IsDBNull(sdr.GetOrdinal("Password"));
                            Console.WriteLine("ID:{0},Name:{1},PWD:{2}", id, Name, PWD);
                            Console.WriteLine(sdr.GetString(1));
                        }
                    }
                }
                conn.Close();//此处可以省略,Dispose()方法会自动检查
            }
        }
    }
}

标签:c#,mdf
0
投稿

猜你喜欢

  • eclipse如何搭建Springboot项目详解

    2023-11-29 04:52:36
  • Spring Security配置保姆级教程

    2023-11-07 11:46:56
  • Android App中实现简单的刮刮卡抽奖效果的实例详解

    2023-07-12 19:06:44
  • Android SeekBar实现平滑滚动

    2022-01-21 10:51:24
  • Springcloud微服务架构基础知识解析

    2023-01-26 14:37:10
  • 不规范使用ThreadLocal导致bug分析解决

    2023-11-24 20:03:59
  • 浅谈Java面向接口编程

    2021-07-25 16:29:58
  • Unity2019-2020 个人版官方免费激活详细方法

    2023-12-08 21:57:39
  • 详解Spring Bean的配置方式与实例化

    2022-01-13 05:47:51
  • C#实现xml文件反序列化读入数据到object的方法

    2023-08-20 00:43:08
  • Android实现二级列表购物车功能

    2022-08-28 09:26:28
  • 使用Java8实现模板方法模式的改造

    2021-10-28 23:46:09
  • 解决Tomcat启动报异常java.lang.ClassNotFoundException问题

    2023-07-27 03:33:01
  • Java Swing组件文件选择器JFileChooser简单用法示例

    2021-09-23 21:00:34
  • C#如何使用Bogus创建模拟数据示例代码

    2023-10-11 10:57:50
  • Java中左移和右移问题图文详解

    2023-02-14 15:44:05
  • Java 实现简单静态资源Web服务器的示例

    2021-06-16 17:12:55
  • C#使用NPOI导入Excel的方法详解

    2021-08-29 07:39:35
  • java求100以内的素数示例分享

    2021-06-30 21:34:39
  • 解决Android studio3.6安装后gradle Download失败(构建不成功)

    2021-07-20 15:38:42
  • asp之家 软件编程 m.aspxhome.com