PDA

View Full Version : خطای دیتابیس موقع Fill کردن DataSet



eshpilen
چهارشنبه 12 مرداد 1390, 21:08 عصر
این برنامه:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Data.SqlClient;
using System.Windows.Forms;

// This form demonstrates using a BindingNavigator to display
// rows from a database query sequentially.
public class Form1 : Form
{
// This is the BindingNavigator that allows the user
// to navigate through the rows in a DataSet.
BindingNavigator customersBindingNavigator = new BindingNavigator(true);

// This is the BindingSource that provides data for
// the Textbox control.
BindingSource customersBindingSource = new BindingSource();

// This is the TextBox control that displays the CompanyName
// field from the the DataSet.
TextBox companyNameTextBox = new TextBox();

public Form1()
{
// Set up the BindingSource component.
this.customersBindingNavigator.BindingSource = this.customersBindingSource;
this.customersBindingNavigator.Dock = DockStyle.Top;
this.Controls.Add(this.customersBindingNavigator);

// Set up the TextBox control for displaying company names.
this.companyNameTextBox.Dock = DockStyle.Bottom;
this.Controls.Add(this.companyNameTextBox);

// Set up the form.
this.Size = new Size(800, 200);
this.Load += new EventHandler(Form1_Load);
}

void Form1_Load(object sender, EventArgs e)
{
// Open a connection to the database.
// Replace the value of connectString with a valid
// connection string to a Northwind database accessible
// to your system.
string connectString = @"Server=.\SQLExpress;Integrated Security=true;User Id=hm;Password=;AttachDBFilename=NORTHWND.MDF";

using (SqlConnection connection = new SqlConnection(connectString))
{

SqlDataAdapter dataAdapter1 =
new SqlDataAdapter(new SqlCommand("Select * From Customers", connection));
DataSet ds = new DataSet();
ds.Tables.Add("Customers");
dataAdapter1.Fill(ds.Tables["Customers"]);

// Assign the DataSet as the DataSource for the BindingSource.
this.customersBindingSource.DataSource = ds.Tables["Customers"];

// Bind the CompanyName field to the TextBox control.
this.companyNameTextBox.DataBindings.Add(
new Binding("Text",
this.customersBindingSource,
"CompanyName",
true));
}
}

[STAThread]
public static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}

موقع اجرا این خطا رو میده:

An attempt to attach an auto-named database for file NORTHWND.MDF failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

از خط dataAdapter1.Fill(ds.Tables["Customers"]); این خطا رو میگیره.

مشکل کجاست؟
ضمنا این کد مثال خود MSDN هست در این صفحه: http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingnavigator.aspx

eshpilen
پنج شنبه 13 مرداد 1390, 21:18 عصر
کانکشن استرینگ رو عوض کردم:

@"Server=.\SQLExpress;Integrated Security=SSPI;Initial Catalog=Northwind;User Instance=true";

اما حالا این خطا رو میده:

Cannot open database "Northwind" requested by the login. The login failed.
Login failed for user '***\***'.


جدا مگه کسی در اینجا تاحالا با اینا کار نکرده و نمیدونه باید چیکار کرد؟
این دیتابیس Northwind و اینا هم که Sample های استاندارد و قدیمی میکروسافت هستن.
بنده برای این خطا گوگل کردم و مقداری مطلب خوندم و کانکشن استرینگ و بخشهای دیگر رو تغییرات مختلفی دادم، اما به نتیجه ای نرسیدم.

eshpilen
جمعه 14 مرداد 1390, 10:31 صبح
یه واقعه عجیب!
با این کانکشن استرینگ:

@"Server=.\SQLExpress;Integrated Security=true;Initial Catalog=Northwind;User Instance=true;AttachDBFilename=.\NORTHWND.MDF";

این خطا رو میده:

Database 'C:\Documents and Settings\***\My Documents\Visual Studio 2010\WebSites\WebSite18\App_Data\nw.mdf' already exists. Choose a different database name.
Cannot attach the file '.\NORTHWND.MDF' as database 'Northwind'.

حالا بعد از اینکه کلی بررسی کردم نتونستم ارتباط بین این پروژهء Windows Forms و اون پروژهء وب سایت WebSite18 رو پیدا کنم. بخاطر همین فکر میکنم شاید ویژوال استودیو یا یه جای دیگه قاط زده!
تمام فایلهای پروژه رو نگاه کردم، اما هیچ ارتباط و اثری از WebSite18 توش نبود.
این پیام خطا چه ارتباطی داره به پروژم و از کجا اومده نمیدونم! شما نظری دارید؟
WebSite18 یه پروژهء مثال وبسایت مثل بقیه بوده که چند وقت پیش بر اساس مثالهای MSDN درستش کردم. تازه الان nw.mdf هم در اون آدرسی که زده وجود نداره!