我想做一个自动生成交易没有。在每次我在sql数据库中插入数据时,在sql数据库中,如学生ID或员工ID。我现在能做的就是插入数据。但我不知道如何创建交易号。在每次我添加数据。
Dim query As String = String.Empty
query = "insert into marketingLog(TransCode,Endorser,Enrollee_Name,Course,Rebates,Trans_Date,CreatedBy,Status)" _
& "values(@transcode,@endorser,@name,@course,@rebates,@transdate,@user,@status)"
Using conn As New SqlConnection(dbUse.db)
conn.Open()
Using comm As New SqlCommand
With comm
.Connection = conn
.CommandType = CommandType.Text
.CommandText = query
.Parameters.Add("@endorser", SqlDbType.VarChar).Value = cmbEndorser.Text 'Endorser
.Parameters.Add("@name", SqlDbType.VarChar).Value = txtEnrolleeName.Text 'Enrolle_Name
.Parameters.Add("@course", SqlDbType.VarChar).Value = cmbCourse.Text 'Course
.Parameters.Add("@rebates", SqlDbType.Int).Value = lblRebate.Text 'Rebates
.Parameters.Add("@transdate", SqlDbType.DateTime).Value = DateAndTime.Now
.Parameters.Add("@user", SqlDbType.VarChar).Value = lblUsername.Text
.Parameters.Add("@status", SqlDbType.VarChar).Value = "Standby"
End With
Try
comm.ExecuteNonQuery()
conn.Close()
loadmyGrid()
loadDefault()
Catch ex As Exception
MessageBox.Show(ex.Message.ToString(), "Error Message")
End Try
End Using
End Using
TransCode是我的专栏名称。我希望每次交易都添加1,2,3,4,5。我用它作为我的交易号。
答案 0 :(得分:1)
在将数据插入到表格中之前,您会遇到什么问题
Select Max(TranCode) From marketingLog
并且它将首次返回一些int数字或0。
你只需用1递增它。
然后执行插入。
实际上我不知道如何在vb中编码但是我想在你的代码中编写逻辑请参考它并尝试使用vb进行应用
例如
Dim query As String = String.Empty
query = "Select Max(TranCode) From marketingLog";
Dim dtb As New DataTable;
Using conn As New SqlConnection(dbUse.db)
conn.Open()
Using dad As New SqlDataAdapter(query, conn)
dad.Fill(dtb)
Dim intTransNo As Integer = Int32.Parse(dtb.Rows(0)(0).ToString()) + 1;
query = "insert into marketingLog(TransCode,Endorser,Enrollee_Name,Course,Rebates,Trans_Date,CreatedBy,Status)" _
& "values(@transcode,@endorser,@name,@course,@rebates,@transdate,@user,@status)"
Using comm As New SqlCommand
With comm
.Connection = conn
.CommandType = CommandType.Text
.CommandText = query
.Parameters.Add("@transcode", SqlDbType.VarChar).Value = intTransNo
.Parameters.Add("@endorser", SqlDbType.VarChar).Value = cmbEndorser.Text 'Endorser
.Parameters.Add("@name", SqlDbType.VarChar).Value = txtEnrolleeName.Text 'Enrolle_Name
.Parameters.Add("@course", SqlDbType.VarChar).Value = cmbCourse.Text 'Course
.Parameters.Add("@rebates", SqlDbType.Int).Value = lblRebate.Text 'Rebates
.Parameters.Add("@transdate", SqlDbType.DateTime).Value = DateAndTime.Now
.Parameters.Add("@user", SqlDbType.VarChar).Value = lblUsername.Text
.Parameters.Add("@status", SqlDbType.VarChar).Value = "Standby"
End With
Try
comm.ExecuteNonQuery()
conn.Close()
loadmyGrid()
loadDefault()
Catch ex As Exception
MessageBox.Show(ex.Message.ToString(), "Error Message")
End Try
End Using
End Using
答案 1 :(得分:0)
CREATE TABLE Persons
(
ID int NOT NULL AUTO_INCREMENT,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
PRIMARY KEY (ID)
)
您可以在表格中使用AUTO_INCREMENT
列。您不需要为此列指定任何数据。此列的数据将自动插入到新记录插入中。
INSERT INTO Persons (FirstName,LastName)
VALUES ('Lars','Monsen')
您的代码:您只需要跳过将数据放入Auto_Increment列。
Dim query As String = String.Empty
//query = "Select Max(TranCode) From marketingLog";
Dim dtb As New DataTable;
Using conn As New SqlConnection(dbUse.db)
conn.Open()
query = "insert into marketingLog(Endorser,Enrollee_Name,Course,Rebates,Trans_Date,CreatedBy,Status)" _
& "values(@endorser,@name,@course,@rebates,@transdate,@user,@status)"
Using comm As New SqlCommand
With comm
.Connection = conn
.CommandType = CommandType.Text
.CommandText = query
.Parameters.Add("@endorser", SqlDbType.VarChar).Value = cmbEndorser.Text 'Endorser
.Parameters.Add("@name", SqlDbType.VarChar).Value = txtEnrolleeName.Text 'Enrolle_Name
.Parameters.Add("@course", SqlDbType.VarChar).Value = cmbCourse.Text 'Course
.Parameters.Add("@rebates", SqlDbType.Int).Value = lblRebate.Text 'Rebates
.Parameters.Add("@transdate", SqlDbType.DateTime).Value = DateAndTime.Now
.Parameters.Add("@user", SqlDbType.VarChar).Value = lblUsername.Text
.Parameters.Add("@status", SqlDbType.VarChar).Value = "Standby"
End With
Try
comm.ExecuteNonQuery()
conn.Close()
loadmyGrid()
loadDefault()
Catch ex As Exception
MessageBox.Show(ex.Message.ToString(), "Error Message")
End Try
End Using
End Using