0

can someone help me.

I've tried moving the MDF file around to different locations but I'm still unable to update the database. I'm using Windows 7.

Here's my code:

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");
        }
    }
}

The code is from a tutorial, I need to get this working before I add a database to my project.

Thank you guys.

1
  • I would attach the MDF file to the SQL Server Express you have running on your machine, and stop using the AttachDbFileName=....;UserInstance=.... stuff. Once you've attached it, you can connect to it using server=.\SQLExpress;database=MyWorkers;integrated security=SSPI; and that's all you need Commented May 17, 2011 at 9:06

1 Answer 1

1

According to your code (in the connection string) the mdf file should be in

C:\Users\rebdog\AppData\MyWorkers.mdf

The connection is set up to use integrated security, meaning that it uses your windows login to access that database. So if you have problems accessing the database, it might be because it requires another user account, or because your user doesn't have read/write access to that folder.

Sign up to request clarification or add additional context in comments.

3 Comments

\ is a special character, so if i put C:\Users\rebdog\AppData\MyWorkers.mdf i get errors. Ive checked premissions and they all able to read plus write.
if you put a '\' in the connectionstring you should escape it so each would be '\\' meaning that the path in the connectionstring will be "C:\\Users\\rebdog\\AppData\\MyWorkers.mdf"
Well for some reason it just started working the way it is now. Gotta love windows 7

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.