当我运行这段代码时,我收到错误:
尚未初始化ExecuteScalar连接属性
我似乎无法找到连接无效的原因。
代码:
Protected Sub btnTrackRepair_Click(sender As Object, e As EventArgs) Handles btnTrackRepair.Click
Using conn As New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\ITrepair.mdf;Integrated Security=True")
conn.Open()
Dim cmd As New SqlCommand(conn.ToString)
Dim txtTracking As String
cmd.CommandText = "SELECT Repair_Status FROM Repair WHERE Tracking_Number =" & txtTrack.Text
txtTracking = If(IsDBNull(cmd.ExecuteScalar), "", cmd.ExecuteScalar)
If txtTracking <> "" Then
MsgBox("Record Found!", MsgBoxStyle.Information, "Update")
txtStatus.Text = ""
txtStatus.Text = txtTracking
Else
MsgBox("No Record Found!", MsgBoxStyle.Information, "INFO.")
End If
End Using
End Sub
代码在txtTracking = If(IsDBNull(cmd.ExecuteScalar), "", cmd.ExecuteScalar)
我已经查看了有关此错误的其他问题,但大多数是C#,我使用的是VB.NET,因此我发现很难找到解决方案。
答案 0 :(得分:1)
您正在使用仅占用字符串的SqlCommand
constructor。但这不是连接字符串,而是查询的文本。所以你做错了。
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Test extends Application{
@Override
public void start(Stage primaryStage) throws Exception {
GridPane root = new GridPane();
root.setAlignment(Pos.CENTER);
root.setHgap(20);
VBox container = new VBox();
container.setAlignment(Pos.CENTER);
final ToggleGroup group = new ToggleGroup();
RadioButton rb1 = new RadioButton("First Choice");
rb1.setToggleGroup(group);
RadioButton rb2 = new RadioButton("Second Choice");
rb2.setToggleGroup(group);
RadioButton rb3 = new RadioButton("Third Choice");
rb3.setToggleGroup(group);
container.getChildren().addAll(rb1,rb2,rb3);
Button btn = new Button("I should appear");
btn.setVisible(false);
root.add(container, 0, 0);
root.add(btn, 1, 0);
group.selectedToggleProperty().addListener(new ChangeListener<Toggle>(){
public void changed(ObservableValue<? extends Toggle> ov,
Toggle old_toggle, Toggle new_toggle) {
if (group.getSelectedToggle() != null) {
btn.setVisible(true);
}
}
});
Scene s = new Scene(root,400,200 );
primaryStage.setScene(s);
primaryStage.setResizable(false);
primaryStage.setTitle("Example");
primaryStage.show();
}
public static void main(String[] args){
launch();
}
}
除此之外,您应该熟悉参数化查询。不要使用字符串连接来构建查询以避免(除其他问题)sql注入攻击。
这是完整的方法:
Dim cmd As New SqlCommand("SELECT Repair_Status FROM Repair WHERE Tracking_Number = @Tracking_Number", conn)
如果数据库中的Protected Sub btnTrackRepair_Click(sender As Object, e As EventArgs)
Dim sqlQuery = "SELECT Repair_Status FROM Repair WHERE Tracking_Number = @Tracking_Number"
Using conn As New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\ITrepair.mdf;Integrated Security=True")
conn.Open()
Dim cmd As New SqlCommand(sqlQuery, conn)
cmd.Parameters.Add("@Tracking_Number", SqlDbType.NVarChar).Value = txtTrack.Text
Dim statusObj = cmd.ExecuteScalar()
Dim status = If(statusObj is DBNull.Value, Nothing, DirectCast(statusObj, string))
If not String.IsNullOrEmpty(status) Then
MsgBox("Record Found!", MsgBoxStyle.Information, "Update")
' ... '
Else
MsgBox("No Record Found!", MsgBoxStyle.Information, "INFO.")
End If
End Using
End Sub
不是varchar / nvarchar而是Tracking_Number
(例如),则应该在此解析它并使用正确的int
。