无法在数据库中添加记录 [英] Cannot add a record in database

查看:61
本文介绍了无法在数据库中添加记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,程序员在这里遇到了一个小问题,如您所见,我想在数据库中添加一条记录(同一所大学,相同的代码,但中心不同).因此,当我单击提交"按钮时,有2条记录被保存在数据库中. /> 例如.

Hello programmers, got a small problem here as you see i want to add a records on the database(same college, same code but different in centers).So when I click submit button 2 records are saved in my database
EX.

i put text on textboxes           Hidden variables of centers
college = wew                     123
code = wew                        456
centers = (based on hidden variables)
SUBMIT

And when I look on the database
College                 Code               Center
wew                     wew                123
wew                     wew                456


多数民众赞成在我想要的,但有一个问题.当我点击提交"按钮时发生错误
Invalid object name ''tblTraningPlan''. 指向command.executenonquery
请帮我..我忘了什么吗?
谢谢和更多强大的程序员
我的代码:


thats what i want, but got a problem. When I hit submit button an error occurs
Invalid object name ''tblTraningPlan''. pointing at command.executenonquery
Plss help me.. Did I forgot something?
Thanks and more power programmers
MY CODES:

Imports System.Math
Imports System.Web
Imports System.Data.SqlClient
Imports System.Data
Partial Class Gee51
    Inherits System.Web.UI.Page

    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        databind15029()
        databind12934()
    End Sub
    Public Sub databind15029()
        Dim strSQL As String
        Dim connection As SqlConnection = New SqlConnection("Data Source=ML0003135586;Initial Catalog=TestSQL;Integrated Security=True")
        strSQL = "INSERT INTO tblTraningPlan(CostCenter, College, Category, CourseCode, CourseTitle, Duration) VALUES (@CostCenter, @College, @Category, @CourseCode, @CourseTitle, @Duration)"
        connection.Open()
        Dim command As New SqlCommand(strSQL, connection)
        command.Parameters.Add("@CostCenter", SqlDbType.NVarChar).Value = lbl15029.Text
        command.Parameters.Add("@College", SqlDbType.NVarChar).Value = txtCollege.Text
        command.Parameters.Add("@Category", SqlDbType.NVarChar).Value = lblGEE.Text
        command.Parameters.Add("@CourseCode", SqlDbType.NVarChar).Value = txtCCode.Text
        command.Parameters.Add("@CourseTitle", SqlDbType.NVarChar).Value = txtCT.Text
        command.Parameters.Add("@Duration", SqlDbType.NVarChar).Value = txtDuration.Text
        command.ExecuteNonQuery()
        connection.Close()
    End Sub
    Public Sub databind12934()
        Dim strSQL As String
        Dim connection As SqlConnection = New SqlConnection("Data Source=ML0003135586;Initial Catalog=TestSQL;Integrated Security=True")
        strSQL = "INSERT INTO tblTraningPlan(CostCenter, College, Category, CourseCode, CourseTitle, Duration) VALUES (@CostCenter, @College, @Category, @CourseCode, @CourseTitle, @Duration)"
        connection.Open()
        Dim command As New SqlCommand(strSQL, connection)
        command.Parameters.Add("@CostCenter", SqlDbType.NVarChar).Value = lbl12934.Text
        command.Parameters.Add("@College", SqlDbType.NVarChar).Value = txtCollege.Text
        command.Parameters.Add("@Category", SqlDbType.NVarChar).Value = lblGEE.Text
        command.Parameters.Add("@CourseCode", SqlDbType.NVarChar).Value = txtCCode.Text
        command.Parameters.Add("@CourseTitle", SqlDbType.NVarChar).Value = txtCT.Text
        command.Parameters.Add("@Duration", SqlDbType.NVarChar).Value = txtDuration.Text
        command.ExecuteNonQuery()
        connection.Close()
    End Sub
End Class

推荐答案

除我假设的是标签控件(lbl12934和lbl15029)外,这两种方法都是相同的.您应该合并它们,而不是复制对象.至少要集中连接字符串和sql语句.

您是否已验证数据库是正确的?名称正确吗?您可以使用它吗?您有插入权吗?
With the exception of, what I assume are label controls (lbl12934 & lbl15029), both of these methods are the same. You should combine them rather than duplicating the objects. At the very least centralize the connection string and sql statement.

Have you verified the database is correct? The name is correct? DO you have access to it? Do you have insert rights?


这篇关于无法在数据库中添加记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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