无法打开sql server连接 [英] could not open sql server connection

查看:107
本文介绍了无法打开sql server连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个Web应用程序中开发了一个Web应用程序有8个文本框

我也有sql server2005我创建了一个数据库我写了

代码日期库连接和应用程序运行时间sqlserver

数据库没有连接如何解决但没有给出任何错误我被复制

代码





i am develop one web application in this web application has 8 textboxes
and also i have the sql server2005 i created the one database i write
the code date base connection and application is running time the sqlserver
database not connected how to solve but not given the any error i am copied
code


protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection connect=new SqlConnection("Data Source=ADMIN-E41B7B0DB\\SQLEXPRESS;Initial Catalog=agfl;connect timeout=30;Integrated Security=True");
           SqlCommand mycommand;
           
            mycommand= new SqlCommand("Insert into pac(sno,rdate,acno,name,vno,amt,chno,edate) values('"+TextBox1.Text+"','"+TextBox5.Text+"','"+TextBox2.Text+"','"+TextBox6.Text+"','"+TextBox3.Text+"','"+TextBox7.Text+"','"+TextBox4.Text+"','"+TextBox8.Text+"')", connect );
             connect.Open();
            mycommand.Parameters.AddWithValue("rdate", TextBox5.Text);
            mycommand.Parameters.AddWithValue("sno", TextBox1.Text);
            mycommand.Parameters.AddWithValue("acno", TextBox2.Text);
            mycommand.Parameters.AddWithValue("name", TextBox6.Text);
            mycommand.Parameters.AddWithValue("vno", TextBox3.Text);
            mycommand.Parameters.AddWithValue("amt", TextBox7.Text);
            mycommand.Parameters.AddWithValue("chno", TextBox4.Text);
            mycommand.Parameters.AddWithValue("edate", TextBox8.Text);
            mycommand.ExecuteNonQuery();        
            connect.Close();
            
                       

        }

推荐答案

两个原因:

1)连接已创建,但从未打开过,

2)命令已创建,但没有参数。



有你听说过 SQLInjection [ ^ ]?请阅读这些文章:

如何:在ASP中防止SQL注入。 NET [ ^ ]

在停止之前阻止SQL注入攻击 [ ^ ]



我建议你使用存储过程:

如何:致电使用ADO.NET和Visual C#.NET进行参数化存储过程 [ ^ ]

如何:执行存储返回无值的过程 [ ^ ]
Two reasons:
1) connection has been created, but never opened,
2) command has been created but does not have parameters.

Have you ever heard about SQLInjection[^]? Please, read these articles:
How To: Protect From SQL Injection in ASP.NET[^]
Stop SQL Injection Attacks Before They Stop You[^]

I would suggest you to use stored procedure:
HOW TO: Call a Parameterized Stored Procedure by Using ADO.NET and Visual C# .NET[^]
How to: Execute a Stored Procedure that Returns No Value[^]


我想你的 ConnectionString 是错误的。



尝试使用正确的,参考 - SQL Server 2005连接字符串 [ ^ ]。



还使用参数化查询/存储过程以避免 SQLInjection 正如 Maciej Los
I guess your ConnectionString is wrong.

Try to use the correct one, refer - SQL Server 2005 connection strings[^].

Also use Parameterized Query/Stored Procedure in order to avoid SQLInjection as suggested by Maciej Los


所建议的那样检查sql server express的服务是否正在运行,如果没有则启动服务。
check the servive of sql server express is it running if not then start service .


这篇关于无法打开sql server连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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