有人可以幫助我。只讀數據庫MDF文件。 Windows應用程序
我試着將MDF文件移到不同的位置,但我仍然無法更新數據庫。我使用的是Windows 7
這裏是我的代碼:
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace TestDatabase
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
System.Data.SqlClient.SqlConnection con;
DataSet ds1;
System.Data.SqlClient.SqlDataAdapter da;
int MaxRows = 0;
int inc = 0;
private void Form1_Load(object sender, EventArgs e)
{
con = new System.Data.SqlClient.SqlConnection();
ds1 = new DataSet();
con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\rebdog\\AppData\\MyWorkers.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
con.Open();
string sql = "SELECT * From tblWorkers";
da = new System.Data.SqlClient.SqlDataAdapter(sql, con);
MessageBox.Show("database Open");
da.Fill(ds1, "Workers");
NavigateRecords();
MaxRows = ds1.Tables["Workers"].Rows.Count;
con.Close();
MessageBox.Show("database closed");
}
private void btnSave_Click(object sender, EventArgs e)
{
System.Data.SqlClient.SqlCommandBuilder cb;
cb = new System.Data.SqlClient.SqlCommandBuilder(da);
DataRow dRow = ds1.Tables["Workers"].NewRow();
dRow[1] = textBox1.Text;
dRow[2] = textBox2.Text;
dRow[3] = textBox3.Text;
ds1.Tables["Workers"].Rows.Add(dRow);
MaxRows = MaxRows + 1;
inc = MaxRows - 1;
da.Update(ds1,"Workers");
}
}
}
的代碼是從一個教程,我需要之前,我將數據庫添加到我的項目得到這個工作。
謝謝你們。
你有什麼錯誤嗎? – Blazes 2011-05-17 08:54:36
我會將MDF文件附加到已在您的計算機上運行的SQL Server Express,並停止使用'AttachDbFileName = ....; UserInstance = ....'東西。一旦你連接它,你可以使用'server =。\ SQLExpress; database = MyWorkers;集成安全性= SSPI;'連接到它並且這就是你需要的一切 – 2011-05-17 09:06:22