我该如何解决这个问题? [英] How do I resolve this issue ?

查看:85
本文介绍了我该如何解决这个问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

没有错误和警告,但我没有得到输出。



显示:



异常抛出:System.Data.dll中的'System.Data.SqlClient.SqlException'

System.Data.dll中发生了'System.Data.SqlClient.SqlException'类型的异常,但未在用户代码

建立与SQL Server的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确,以及SQL Server是否配置为允许远程连接。 (提供者:命名管道提供程序,错误:40 - 无法打开与SQL Server的连接)



'iisexpress.exe'(CLR v4.0.30319:/ LM / W3SVC / 2 / ROOT-1-131831092354667571):已加载'C:\Program Files(x86)\ Microsoft Visual Studio \ 2017 \Community \Common7 \IDE \ Private`大会\ Runtime\Microsoft.VisualStudio .Debugger.Runtime.dll。跳过加载符号。模块已经过优化,调试器选项Just My Code已启用。

程序'[14544] iisexpress.exe'已退出,代码为-1(0xffffffff)。



我尝试了什么:



命名空间Emp

{

public partial class DBfetch:System.Web.UI.Page

{

public static int i;

SqlConnection conn = null;

SqlDataAdapter da;

DataTable dt = new DataTable();

DataRow dr;

protected void Page_Load(object sender,EventArgs e)

{

string connectionstring =Data Source = DESKTOP-KR73POJ; Initial Catalog = Test; Persist Security Info = True; User ID = sa;密码= admin;

conn = new SqlConnection(connectionstring);

conn.Open();

da = new SqlDataAdapter (select * from testtable,conn);

da.Fill(dt);

conn.Close();

}

protected void Button1_Click(object sender,EventArgs e)

{

i = 0;

dr = dt.Rows [i];

TextBox1.Text = Convert.ToString (dr [0]);

TextBox2.Text = Convert.ToString(dr [1]);

TextBox3.Text = Convert.ToString(dr [2]) ;

}

protected void Button2_Click(object sender,EventArgs e)

{

if(i == 0)

Response.Write(First record!);

else

i - ;

dr = dt.Rows [i];

TextBox1.Text = Convert.ToString(dr [0]);

TextBox2.Text = Convert.ToString(dr [1 ]);

TextBox3.Text = Convert.ToString(dr [2]);

}

protected void Butt on3_Click(object sender,EventArgs e)

{

if(i ==(dt.Rows.Count - 1))

响应。写(最后记录!);

其他

i ++;

dr = dt.Rows [i];

TextBox1.Text = Convert.ToString(dr [0]);

TextBox2.Text = Convert.ToString(dr [1]);

TextBox3.Text = Convert.ToString(dr [2]);

}



}



}

No errors and warnings but i am not getting the output.

showing:

Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.dll
An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code
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)

'iisexpress.exe' (CLR v4.0.30319: /LM/W3SVC/2/ROOT-1-131831092354667571): Loaded 'C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\PrivateAssemblies\Runtime\Microsoft.VisualStudio.Debugger.Runtime.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
The program '[14544] iisexpress.exe' has exited with code -1 (0xffffffff).

What I have tried:

namespace Emp
{
public partial class DBfetch : System.Web.UI.Page
{
public static int i;
SqlConnection conn=null;
SqlDataAdapter da;
DataTable dt = new DataTable();
DataRow dr;
protected void Page_Load(object sender, EventArgs e)
{
string connectionstring = "Data Source=DESKTOP-KR73POJ;Initial Catalog=Test; Persist Security Info=True;User ID=sa;Password=admin";
conn = new SqlConnection(connectionstring);
conn.Open();
da = new SqlDataAdapter("select * from testtable", conn);
da.Fill(dt);
conn.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
i = 0;
dr = dt.Rows[i];
TextBox1.Text = Convert.ToString(dr[0]);
TextBox2.Text = Convert.ToString(dr[1]);
TextBox3.Text = Convert.ToString(dr[2]);
}
protected void Button2_Click(object sender, EventArgs e)
{
if (i == 0)
Response.Write("First record !");
else
i--;
dr = dt.Rows[i];
TextBox1.Text = Convert.ToString(dr[0]);
TextBox2.Text = Convert.ToString(dr[1]);
TextBox3.Text = Convert.ToString(dr[2]);
}
protected void Button3_Click(object sender, EventArgs e)
{
if (i == (dt.Rows.Count - 1))
Response.Write("Last record !");
else
i++;
dr = dt.Rows[i];
TextBox1.Text = Convert.ToString(dr[0]);
TextBox2.Text = Convert.ToString(dr[1]);
TextBox3.Text = Convert.ToString(dr[2]);
}

}

}

推荐答案

查看错误消息:

Look at the error message:
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)

很明显:它找不到您所指的SQL服务器实例。

这意味着您的连接字符串是错误的。

忽略您不应该对连接字符串进行硬编码,它们应该在配置文件中,这样您每次发布时都不必更改应用程序...



从这里开始:简单的SQL连接字符串创建 [ ^ ]并确保在开始编码之前连接正常工作!

It's pretty clear: it can't find the SQL server instance you are referring to.
Which means your connection string is wrong.
Ignoring that you shouldn;t hardcode connection strings, they should be in config files so you don't have to change the app each time you release it...

Start here: Simple SQL Connection String Creation[^] and make sure the connection works before you start to code!


这篇关于我该如何解决这个问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆