C# 使用基于服务的数据库的使用注意

Data Developer Center > Data Platform Development Forums > ADO.NET DataSet 上有一篇问答讲的很好

这是她的网址http://social.msdn.microsoft.com/Forums/en/adodotnetdataset/thread/1faff35e-055b-4728-a6c8-ece257585ab7

在我们平时的简单应用中,或者是刚开始学习使用ADO.NET,用基于服务的数据库会方便一些,但也给我们带来了一些困难,这里我重点讲一个好多新手会遇到的问题,

那就是,我建立了一个基于服务的数据库 database1.mdf 

string connstr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using(SqlConnection conn=new SqlConnection(connstr))
{
conn.Open();
using(SqlCommand cmd=conn.CreateCommand())
{
cmd.CommandText = "delete from T_Mobile";
cmd.ExecuteNonQuery();

string[] files = Directory.GetFiles(path, "*txt", SearchOption.TopDirectoryOnly);
foreach (string file in files)
{
//string title=Path.GetFileNameWithoutExtension();
using(FileStream fileStream=File.OpenRead(file))
{
using(StreamReader streamReader=new StreamReader(fileStream,System.Text.Encoding.Default))
{
string line = null;
while ((line=streamReader.ReadLine())!=null)
{
string[] strs = line.Split(',');
int startNo = Convert.ToInt32(strs[1]);
int code = Convert.ToInt32(strs[2]);
string city = strs[3];
string cardType = strs[4];
cmd.Parameters.Clear();
cmd.CommandText = "insert into T_Mobile(startNo,code,city,cardType) values(@startNo,@code,@city,@cardType)";
cmd.Parameters.Add(new SqlParameter("@startNo", startNo));
cmd.Parameters.Add(new SqlParameter("@code", code));
cmd.Parameters.Add(new SqlParameter("@city", city));
cmd.Parameters.Add(new SqlParameter("@cardType", cardType));
cmd.ExecuteNonQuery();
}
}
}
}
}
}

从选取的文件夹中读出了所有她的.txt文件,并把它存入数据库(事实上,这正是我学习ADO.NET时的一个练习—— 手机号码归属地查询)成功后我却没有在表中看见数据,真相很简单,c#默认存储在\bin\Debug下了,所以我们只需在入口函数处添加一段代码

//Program.cs

using System.Windows.Forms;

namespace MobileAscription
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
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);
}


Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}

切记要加载入口函数的最开始!!

转载于:https://www.cnblogs.com/ZJoy/archive/2010/12/17/1909597.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLCipher 是一个基于SQLite的加密扩展,可以用于在C#应用程序中为SQLite数据库提供加密功能。在C#使用SQLCipher加密SQLite数据库需要以下步骤: 1. 下载SQLCipher库文件并将其添加到C#项目中。 2. 在C#代码中使用SQLiteConnection类创建一个SQLite数据库连接,并使用SQLCipher的加密密钥对其进行加密。 ``` using System.Data.SQLite; using System.Security.Cryptography; string databasePath = @"C:\path\to\database.db"; string password = "myPassword"; byte[] salt = new byte[16] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6 }; byte[] key = new Rfc2898DeriveBytes(password, salt).GetBytes(16); SQLiteConnection connection = new SQLiteConnection("Data Source=" + databasePath); connection.SetPassword(key); connection.Open(); ``` 在上面的示例中,我们使用Rfc2898DeriveBytes类从密码和盐中生成16字节的加密密钥,并使用SetPassword方法将其应用于SQLite连接。 3. 执行SQL命令来创建表和插入数据等操作。 ``` SQLiteCommand command = new SQLiteCommand("CREATE TABLE myTable (id INTEGER PRIMARY KEY, name TEXT)", connection); command.ExecuteNonQuery(); command = new SQLiteCommand("INSERT INTO myTable (name) VALUES ('John Doe')", connection); command.ExecuteNonQuery(); ``` 在上面的示例中,我们使用SQLiteCommand类来执行SQL命令。 4. 在完成对数据库的操作后,关闭SQLite连接。 ``` connection.Close(); ``` 以上就是在C#应用程序中使用SQLCipher加密SQLite数据库的步骤。需要注意的是,为了保证安全性,应该使用强密码来生成加密密钥,并且不要将密码硬编码到应用程序中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值