PDA

View Full Version : مبتدی: چرا نمیتونم در سی شارپ یه sql وصل بشم؟ A network-related or instance-specific error



shd.online
سه شنبه 12 تیر 1397, 09:20 صبح
سلام
کدم اینه
public void SqlConnection()
{
// 1. Instantiate the connection
SqlConnection conn = new SqlConnection("Data Source=SHD2/SHD_INSTANCE;Initial Catalog=davarpanah_db;Integrated Security=True");
SqlDataReader rdr = null;
try
{
// 2. Open the connection
conn.Open();


// 3. Pass the connection to a command object
SqlCommand cmd = new SqlCommand("select * from CSDic", conn);


// 4. Use the connection
// get query results
rdr = cmd.ExecuteReader();


// print the CustomerID of each record
while (rdr.Read())
{
richTextBox1.Text = "1";
}
}
finally
{
// close the reader
if (rdr != null)
{
rdr.Close();
}


// 5. Close the connection
if (conn != null)
{
conn.Close();
}
}
}

نام سرور همینه فقط بک اسلش رو کردم اسلش تا خطا نگیره ولی روی open کردن خطا میده:
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll


Additional information: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

sds1920
سه شنبه 12 تیر 1397, 14:01 عصر
اگر SqlServer روی سیستم Local نصب هست به جای
SHD2/SHD_INSTANCE از دات (.) استفاده کنید. اگر سرور روی شبکه هست احتمالا مشکل از DNS Server هست که نمیتوانه اسم رو به IP تبدیل کنه یا مشکل از FQDN که مشخص کردید. پیشنهادم این هست که در این مورد از آدرس IP سرور استفاده کنید ببینید وصل میشه یا نه.

shd.online
چهارشنبه 13 تیر 1397, 08:02 صبح
من وقتی sql نصب کردم و وارد شدم با . و local کار نکرد و نام سرور برای ورود این بود SHD2/SHD_INSTANCE
وقتی از طریق تست در ویژوال استودیو این نام سرور رو میزنم و دکمه تست رو میزنم مینویسه با موفقیت انجام شد ولی توی کد نمیشه
از ip و اینا سر در نمیارم اصلا به شبکه وصل نیستم

رامین مرادی
چهارشنبه 13 تیر 1397, 09:50 صبح
شما بجای نام سرور اینو بزنید
127.0.0.1
یا
(local)

cmsdqq2
جمعه 15 تیر 1397, 16:11 عصر
(localdb)\v11.0




دقت کنید که اول localdb هست و بعد بک اسلش و بعد ورژن

davidrobert
جمعه 15 تیر 1397, 18:36 عصر
سلام
کدم اینه
public void SqlConnection()
{
// 1. Instantiate the connection
SqlConnection conn = new SqlConnection("Data Source=SHD2/SHD_INSTANCE;Initial Catalog=davarpanah_db;Integrated Security=True");
SqlDataReader rdr = null;
try
{
// 2. Open the connection
conn.Open();


// 3. Pass the connection to a command object
SqlCommand cmd = new SqlCommand("select * from CSDic", conn);


// 4. Use the connection
// get query results
rdr = cmd.ExecuteReader();


// print the CustomerID of each record
while (rdr.Read())
{
richTextBox1.Text = "1";
}
}
finally
{
// close the reader
if (rdr != null)
{
rdr.Close();
}


// 5. Close the connection
if (conn != null)
{
conn.Close();
}
}
}

نام سرور همینه فقط بک اسلش رو کردم اسلش تا خطا نگیره ولی روی open کردن خطا میده:
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll


Additional information: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

سلام ابتدا بگم برای اسکیول بایستی حتما آدرس به این صورت باشه

SqlConnection conn = new SqlConnection(@"Data Source=SHD2/SHD_INSTANCE;Initial Catalog=davarpanah_db;Integrated Security=True");



دومین موضوع پیغام مد نظر از خاموش بودن یا ارتباط نداشتن به سرور هستش

davidrobert
جمعه 15 تیر 1397, 18:41 عصر
من وقتی sql نصب کردم و وارد شدم با . و local کار نکرد و نام سرور برای ورود این بود SHD2/SHD_INSTANCE
وقتی از طریق تست در ویژوال استودیو این نام سرور رو میزنم و دکمه تست رو میزنم مینویسه با موفقیت انجام شد ولی توی کد نمیشه
از ip و اینا سر در نمیارم اصلا به شبکه وصل نیستم

درست در این قسمت که شما میگید که زده اتصال برقرار شد رشته اتصال برای شما میسازه که میتوانید کپی و قرار بدید داخل برنامه