English 中文(简体)
如何将数据保存到数据库
原标题:How to save data to Database

我搜索并发现这个代码是为了将数据保存到数据库, 连接字符串是正常的, 没有例外/错误被丢弃, 但我不知道为什么这个代码没有将数据保存到我的数据库中。

    string query = "Insert Into BookConfiguration (BookNum, x_axis, y_axis, BookName) Values (@BookNum, @x_axis, @y_axis, @BookName)";
    string connStr = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\BookShelf.mdf;Integrated Security=True;User Instance=True";


    try
    {
        using (SqlConnection conn = new SqlConnection(connStr))
        {                   
                SqlDataAdapter da = new SqlDataAdapter();
                da.InsertCommand = new SqlCommand(query, conn);
                da.InsertCommand.Parameters.Add("@BookNum", SqlDbType.Int).Value = quantity;
                da.InsertCommand.Parameters.Add("@x_axis", SqlDbType.Int).Value = x;
                da.InsertCommand.Parameters.Add("@y_axis", SqlDbType.Int).Value = y;
                da.InsertCommand.Parameters.Add("@BookName", SqlDbType.Text).Value = openFileDialog1.FileName;
            conn.Open();    

            da.InsertCommand.ExecuteNonQuery();

            conn.Close();
        }
    }

    catch (SqlException ex)
    {
        MessageBox.Show("Error Occured " + ex.Message);
    }

EDIT i have now changed the code same problem

代码 :

        try
        {
            using (TransactionScope scope = new TransactionScope())
            {

                using (SqlConnection conn = new SqlConnection(connStr))
                {
                    conn.Open();
                    using (SqlCommand cmd = new SqlCommand(query, conn))
                    {
                        cmd.Parameters.Add(new SqlParameter("@BookNum", quantity));
                        cmd.Parameters.Add(new SqlParameter("@x_axis", x));
                        cmd.Parameters.Add(new SqlParameter("@y_axis", y));
                        cmd.Parameters.Add(new SqlParameter("@BookName", openFileDialog1.FileName));
                        cmd.ExecuteNonQuery();
                    }
                    conn.Close();
        }

                scope.Complete();
            }
        }

我希望这不含糊...

代码在C# 视觉工作室 2010 sql服务器 2008...

最佳回答

我几乎可以保证,因为你正在使用已折旧的用户实例 / 附加 DbFile Name 特性,所以你正在查看与程序不同的数据库副本。

请见 @marc_s这里的答案,

https://stackoverflow.com/a/10740026/61305

基本上, 停止使用这个无效功能 。 请将您的数据库附加到 SQL 服务器的真实实例上, 然后将您的数据库固定下来, 并且您可以连接到与您的程序相同的副本...

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签