.net c#无法启动连接

Connection can´t be started
2020-10-25
  •  译文(汉语)
  •  原文(英语)

我有一个我无法修复的错误.

 SqlConnection cn = new SqlConnection(@"Data Source=
                 (LocalDB)\v11.0;AttachDbFilename=
                  C:\Users\gpsi1_000\Desktop\Pap 
                  Fábio\db\clientes.mdf;Integrated Security=True;
                  Connect Timeout=30");
 SqlCommand cmd = new SqlCommand();

这是我连接数据库的方式:

if (txtnovocp.Text != "" & txtnovamora.Text != "" 
               & txtnovaloca.Text != "" & txtnovoconce.Text != "")
 {
    cn.Open();
    cmd.CommandText = "INSERT INTO Postal   
                     (Codigo_Postal,Morada,Localidade,Concelho) 
                     VALUES('" + txtnovocp.Text + "','" +
                     txtnovamora.Text + "', '" + txtnovaloca.Text 
                      + "' , '" + txtnovoconce.Text + "')";
    cmd.ExecuteNonQuery();
    cmd.Clone();
    MessageBox.Show("Código-Postal Inserido");
    cn.Close();
    txtnovocp.Text = "";
    txtnovaloca.Text = "";
    txtnovamora.Text = "";
    txtnovoconce.Text = "";
  }

这是我要在邮政中插入的代码

但是当我单击按钮保存值时,显示一个错误:

An unhandled exception of type 'System.InvalidOperationException' 
occurred in System.Data.dll
Additional information: ExecuteNonQuery: 
A propriedade Connection não foi inicializada.
速聊1:
虽然你不是传递cncmd任何地方?以其构造函数为例?-另外,您还需要对其进行参数化,因为它容易受到SQL注入的攻击.
速聊2:
如果我在所有行中都输入cn会显示错误
速聊3:
SqlCommand需要知道应该使用什么连接来访问数据库.您需要设置命令的Connection属性,或者更好地使用适当的构造函数
速聊4:
也许您可以花一点精力看一下MSDN的示例?msdn.microsoft.com/zh-CN/library/…
速聊5:
这是一个基本示例(通过使用slqcommand可以轻松找到)dotnetperls.com/sqlconnection
解决过程1

尝试:

using (var connection =  new SqlConnection...)
{
    connection.Open();
    var cmd = connection.CreateCommand();
    ...
}
解决过程2

用英语错误消息是:

附加信息:ExecuteNonQuery:Connection属性未初始化.

我觉得这很有意义,不是吗?您必须初始化连接并将其分配给命令.

SqlConnection cn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\gpsi1_000\Desktop\Pap Fábio\db\clientes.mdf;Integrated Security=True;Connect Timeout=30");
SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;  // <-----

如果这是该类的成员,则不能将连接分配给命令内联.通常,您使用类的构造函数.

SqlConnection cn = null;
SqlCommand cmd = null;

public ClassName()
{
    cn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\gpsi1_000\Desktop\Pap Fábio\db\clientes.mdf;Integrated Security=True;Connect Timeout=30");
    cmd = new SqlCommand();
    cmd.Connection = cn;
}
速聊1:
错误2无效令牌';' 在类,结构或接口成员声明中
速聊2:
错误1在类,结构或接口成员声明中的无效令牌'='
速聊3:
还有CreateCommand,因此SqlCommand cmd = cn.CreateCommand()在这种情况下无需分配连接.
速聊4:
çalves:如果您的类中的字段使用构造函数,则显示一个示例.

I have an error that i can't fix.

 SqlConnection cn = new SqlConnection(@"Data Source=
                 (LocalDB)\v11.0;AttachDbFilename=
                  C:\Users\gpsi1_000\Desktop\Pap 
                  Fábio\db\clientes.mdf;Integrated Security=True;
                  Connect Timeout=30");
 SqlCommand cmd = new SqlCommand();

This is how i connect to database:

if (txtnovocp.Text != "" & txtnovamora.Text != "" 
               & txtnovaloca.Text != "" & txtnovoconce.Text != "")
 {
    cn.Open();
    cmd.CommandText = "INSERT INTO Postal   
                     (Codigo_Postal,Morada,Localidade,Concelho) 
                     VALUES('" + txtnovocp.Text + "','" +
                     txtnovamora.Text + "', '" + txtnovaloca.Text 
                      + "' , '" + txtnovoconce.Text + "')";
    cmd.ExecuteNonQuery();
    cmd.Clone();
    MessageBox.Show("Código-Postal Inserido");
    cn.Close();
    txtnovocp.Text = "";
    txtnovaloca.Text = "";
    txtnovamora.Text = "";
    txtnovoconce.Text = "";
  }

And this is my code to insert in Postal

but when i click on button to save the values show me an error:

An unhandled exception of type 'System.InvalidOperationException' 
occurred in System.Data.dll
Additional information: ExecuteNonQuery: 
A propriedade Connection não foi inicializada.
Talk1:
Likely you are not passing cn to cmd anywhere? In its constructor for example? - Also you need to parametrize that as its vulnerable to SQL injection.
Talk2:
if i put cn in all lines show me an error
Talk3:
An SqlCommand need to know what connection should use to reach the database. You need to set the Connection property of the command or better use the appropriate constructor
Talk4:
Perhaps with a little effort you could take a look at the examples of MSDN? msdn.microsoft.com/en-us/library/…
Talk5:
here's a basic example (easy to find by googling slqcommand) dotnetperls.com/sqlconnection
Solutions1

Try:

using (var connection =  new SqlConnection...)
{
    connection.Open();
    var cmd = connection.CreateCommand();
    ...
}
Solutions2

In english the error message is:

Additional information: ExecuteNonQuery: The Connection property is not initialized.

I find it very meaningful, isn't it? You have to initialize the connection and assign it to the command.

SqlConnection cn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\gpsi1_000\Desktop\Pap Fábio\db\clientes.mdf;Integrated Security=True;Connect Timeout=30");
SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;  // <-----

If this is a member of the class you cannot assign the connection to the command inline. Normally you use the costructor of the class.

SqlConnection cn = null;
SqlCommand cmd = null;

public ClassName()
{
    cn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\gpsi1_000\Desktop\Pap Fábio\db\clientes.mdf;Integrated Security=True;Connect Timeout=30");
    cmd = new SqlCommand();
    cmd.Connection = cn;
}
Talk1:
Error 2 Invalid token ';' in class, struct, or interface member declaration
Talk2:
Error 1 Invalid token '=' in class, struct, or interface member declaration
Talk3:
There's also CreateCommand, so SqlCommand cmd = cn.CreateCommand(), no need to assign the connection in that case.
Talk4:
çalves: if that are fields in your class use the constructor, shown an example.
转载于:https://stackoverflow.com/questions/30400671/connection-can%c2%b4t-be-started

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入