- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前在创建自定义 ID 时遇到 executeScalar 问题。错误是
InvalidOperationException was unhandled by user code.
Message=Connection must be valid and open.
下面是我的代码:
Dim str As String = "select isnull(max(idcr_record),0)+1 from cr_record where Emplid = '" & Session("Emplid") & "'"
Dim com As New MySqlCommand()
'this line was the error
'
Dim count As Integer = Convert.ToInt32(com.ExecuteScalar())
If count = 1 Then
'insert if no record found
'
Dim cc As New MySqlCommand("insert into cr_record values (" & count & "," & Session("Emplid") & ")")
cc.ExecuteNonQuery()
Else
'update becoz record already exist
'
Dim upd As String = "update cr_record set idcr_record = " & count & " where Emplid = " & Session("Emplid") & " "
Dim cc As New MySqlCommand(upd)
cc.ExecuteNonQuery()
End If
Dim length As Integer = 0
Dim val As String = Convert.ToString(count)
length = val.Length
If length = 1 Then
val = "CR " + "00000" & Convert.ToString(count)
End If
If length = 2 Then
val = "CR " + "0000" & Convert.ToString(count)
End If
If length = 3 Then
val = "CR " + "000" & Convert.ToString(count)
End If
If length = 4 Then
val = "CR " + "00" & Convert.ToString(count)
End If
If length = 5 Then
val = "CR " + "0" & Convert.ToString(count)
End If
cr_id = "" & val
最佳答案
至少,您需要将 SQL 命令字符串和 SqlConnection
对象传递给 SqlCommand
对象,然后才能执行命令:
Dim str As String = "select isnull(max(idcr_record),0)+1 from cr_record where Emplid = '" & Session("Emplid") & "'"
Dim conn As New MySqlConnection
conn.ConnectionString = "....."
'pass your SQL command string and MySqlConnection object to MySqlCommand
'
Dim com As New MySqlCommand(str, conn)
conn.Open()
Dim count As Integer = Convert.ToInt32(com.ExecuteScalar())
关于mysql - 创建自定义 ID 时出现 ExecuteScalar 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22800179/
我正在尝试返回 DbSet哪里Survey定义为 public class Survey { public int Id { get; set; } [StringLength(100
我使用库“Dapper”连接 MS Sql。 当我调用存储过程时,它显示错误信息 can not find stored procedure. 但是存储过程它已经存在于数据库中。我该如何解决这个问题?
给我这个错误连接必须有效且打开我不知道如何解决它。 private void loginUtenti(string user, string psw) { create
我试图在一个 int 变量中获取 sql 查询的结果,但我收到了对象空引用错误。有人可以指导我吗。 oconn = new SqlConnection(oSession.CONNECTION_STR
我有这个模块 strSQL = "Insert Into TblInsurancePersonel (CompId,PersonelNo,FirstName,LastName) "; st
下面的代码有点问题。 NextIPID() 方法应该简单地查看 SQL Server 中的一个表,该表返回字段 IPID 的最大值,然后将其加 1,以获得序列中的下一个数字。 public int N
我正在使用以下 SQL 查询和 ExecuteScalar() 方法从 Oracle 数据库中获取数据: sql = "select username from usermst where useri
我正在尝试使用 ExecuteScalar() 从数据库中返回一个整数。但是,当我在数据库本身上运行查询时,我得到了正确的答案,并且 c# 始终给我一个 0(Null)。我知道它返回一个空值,因为在我
ExecuteScalar会自动关闭连接吗? 最佳答案 不,使用 ExecuteScalar() 时需要显式打开和关闭连接。 关于ADO.NET:ExecuteScalar 是否自动关闭连接?,我们在
我正在使用 SQL Server 2005 和 C#。我想在向数据库中插入记录时获取主键值。 为此,我有一个 C# 方法,我可以在其中插入和更新记录。我正在使用 ExecuteScalar 方法进行插
我正在使用 ExecuteScalar 获取一列: cmd.commandtext = "select rodeuser from customer_db_map"; string rode
ExecuteScalar() 有时返回空对象 - 而不是 null- 尽管记录存在。当我用 quickwatch 分析这个对象时,我看到 object.GetType() 等于 DbNull。我可以
我在同一个 ExecuteScalar 命令中同时检查 2 条记录时遇到问题。 我想在本地数据库中不同列的同一行中一起搜索2条记录,我尝试像下面的代码一样进行操作,但只有在1个条件下执行它才可以,但我
关键是我有这段代码 var ojv = xmd.ExecuteScalar().ToString(); if (ojv != null) { C
当我运行以下代码时: query = "select count(*) from table where name = '?name'"; MySqlConnection connection =
我有一个使用事务的 sql 查询,它使用 return 来指示失败或成功,如下所示: BEGIN DECLARE @intErrorCode INT; DECLARE @emailAu
下面是我的程序,用于测量 ExecuteScalar 进行多次迭代所花费的时间。 static void Main(string[] args) { string sqlConnectionS
我正在用可视化 C# 编写一个程序,我在其中提取数据并将其放入 dataGridView 中。这一切都很好,但如果我之后尝试提取更多数据(我想将数据放入文本框),它会出现错误。 这是我提取数据并将其放
我试过各种方法,但似乎都行不通。由于结果为空,例程一直返回 0。 这是我的代码: string strSql = "SELECT [ID] FROM [PurchaseOrder].[dbo].[FM
我正在使用 Xamarin 中的 MySQL 库连接到我的数据库。我正在调用 ExecuteScalar() 命令来检查我的数据库中是否存在用户。 我将 ExecuteScalar() 的返回转换为
我是一名优秀的程序员,十分优秀!