| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3023 人关注过本帖
标题:这里真的没人能帮我吗,这个问题我已经折腾一个星期了也没解决!
只看楼主 加入收藏
杀虫剂
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2007-12-12
收藏
 问题点数:0 回复次数:22 
这里真的没人能帮我吗,这个问题我已经折腾一个星期了也没解决!
数据就是写不进数据库!!!
也许不是代码问题,但问题可能出在哪里呢?
我在网上也搜了好几天了,没有类似的问题.

请大家帮忙看看这段代码哪里有问题,谢谢!
Shared Function Insert(ByVal ConnectionString As String, ByVal SQLString As String) As Integer
        Dim Connection As SqlConnection = New SqlConnection(ConnectionString)
        Dim Command As SqlCommand = New SqlCommand(SQLString, Connection)
        Dim Transaction As SqlTransaction
        Dim Count As Integer = 0

        Connection.Open()
        Transaction = Connection.BeginTransaction()
        Command.Transaction = Transaction

        Try
            Count = Command.ExecuteNonQuery()
            ()
        Catch ex As SqlException
            MsgBox(ex.ToString())
        Catch ex As Exception
            MsgBox(ex.ToString())
        Finally
            Connection.Close()
        End Try
        Return Count
    End Function

学习VB2005中
调用insert后count的返回值说明数据库操作正常,但就是没有添加进数据.
可我的代码里有commit呀
传进的参数为SQLString = "INSERT INTO Table1(aaa,bbb,ccc) VALUES('a2','b2','c2')"
自己研究好几天了,不知道哪里错了.
谢谢了!!!
搜索更多相关主题的帖子: 折腾 
2007-12-13 10:49
fairy4
Rank: 4
等 级:贵宾
威 望:10
帖 子:738
专家分:267
注 册:2007-11-1
收藏
得分:0 
Shared Function Insert(ByVal ConnectionString As String, ByVal SQLString As String) As Integer
        

        Dim connection As New SqlConnection(ConnectionString)
        Dim command As New SqlCommand()
        Dim transaction As SqlTransaction



        Dim Count As Integer = 0
        command.Connection = connection
        connection.Open()
        transaction = connection.BeginTransaction()
        command.Transaction = transaction
         = SQLString

        Try
            Count = command.ExecuteNonQuery()
            ()
        Catch ex As Exception
            MsgBox(ex.ToString())
        Finally
            connection.Close()
        End Try
        Return Count
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim SQLString As String = "INSERT INTO test(aa,bb,cc) VALUES  ('a2','b2','c2')"
        Dim conn As String = "User ID=test;PassWord=1234;Data Source=127.0.0.1  ;DataBase=Test"
        Call Insert(conn, SQLString)
    End Sub
自己去看,你少了什么!!!!

[[italic] 本帖最后由 fairy4 于 2007-12-13 14:45 编辑 [/italic]]

一个人只有一个心脏,却有两个心房。一个住着快乐;一个住着悲伤。不要笑得太大声,不然会吵醒旁边的悲伤
2007-12-13 14:44
杀虫剂
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2007-12-12
收藏
得分:0 
不同的地方有这些:
1\ Functiong中的 = SQLString ,我试过了,有没有这句结果是一样的.
2\  button1_click的设置:  1. Call Insert(conn, SQLString)这句有没有结果都一样.
                                             2.连接字符串不同
conn = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\练习.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
      这是从MyProject--->设置里面粘下来的,因为其他的设置会出错.(我不太肯定这么做对不对,这个连接字符串是系统自动生成的)
如果是连接字符串的问题,我应该怎么做呢,如果不是......
      问题还是没有解决
      我知道这是我学习上的一个坎,请帮助一下.谢谢了!
2007-12-13 15:26
fairy4
Rank: 4
等 级:贵宾
威 望:10
帖 子:738
专家分:267
注 册:2007-11-1
收藏
得分:0 
图片附件: 游客没有浏览图片的权限,请 登录注册

我是運行成功,你看圖片!

一个人只有一个心脏,却有两个心房。一个住着快乐;一个住着悲伤。不要笑得太大声,不然会吵醒旁边的悲伤
2007-12-13 16:48
杀虫剂
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2007-12-12
收藏
得分:0 
将"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\练习.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"中的Data Source=.\SQLEXPRESS"
改为Data Source=localhost编译时报错.
AttachDbFilename=|DataDirectory|\练习.mdf更改后同样报错.
重新设定SQL server 2005的配置,改成正在运行和自动,还有网络\本地都可以.还是不行.

只要不是系统生成的连接字符串一编译就出错
数据库就是添加不了
高手们快帮帮忙吧!!!!!!!
2007-12-13 16:57
fairy4
Rank: 4
等 级:贵宾
威 望:10
帖 子:738
专家分:267
注 册:2007-11-1
收藏
得分:0 
看來你的連接字符串有問題!!!!


Call Insert(conn, SQLString)这句有没有结果都一样.

??不調用這個,你怎么插數據?

一个人只有一个心脏,却有两个心房。一个住着快乐;一个住着悲伤。不要笑得太大声,不然会吵醒旁边的悲伤
2007-12-13 17:01
fairy4
Rank: 4
等 级:贵宾
威 望:10
帖 子:738
专家分:267
注 册:2007-11-1
收藏
得分:0 
Dim conn As String = "User ID=test;PassWord=1234;Data Source=127.0.0.1  ;DataBase=Test"
試下這句

User ID:用戶名
PassWord:密碼
Data Source:你自己的IP地址
DataBase:數據庫名
你自己進企業管理器有問題沒???????

一个人只有一个心脏,却有两个心房。一个住着快乐;一个住着悲伤。不要笑得太大声,不然会吵醒旁边的悲伤
2007-12-13 17:04
杀虫剂
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2007-12-12
收藏
得分:0 
这样吧,你新建一个工程,添加一个类,把下面的粘过去.
Imports System.Data.SqlClient
Public Class EditDataBase
    Shared Function Insert(ByVal ConnectionString As String, ByVal SQLString As String) As Integer
        Dim Connection As SqlConnection = New SqlConnection(ConnectionString)
        Dim Command As SqlCommand = New SqlCommand(SQLString, Connection)
        Dim Transaction As SqlTransaction
        Dim Count As Integer = 0
        Command.Connection = Connection
        Connection.Open()
        Transaction = Connection.BeginTransaction()
        Command.Transaction = Transaction
        Try
            Count = Command.ExecuteNonQuery()
            ()
        Catch ex As SqlException
            MsgBox(ex.ToString())
        Catch ex As Exception
            MsgBox(ex.ToString())
        Finally
            Connection.Close()
        End Try
        Return Count
    End Function
    Shared Function Delete(ByVal ConnectionString As String, ByVal table As String, ByVal row As String, ByVal value As String) As Integer
        Dim Connection As SqlConnection = New SqlConnection(ConnectionString)
        Dim SQLString As String = "Delete Form" + table + "Where" + row + "=" + "" + value + ""
        Dim Command As SqlCommand = New SqlCommand(SQLString, Connection)
        Dim Count As Integer = 0
        Try
            Connection.Open()
            Count = Command.ExecuteNonQuery()
        Catch ex As SqlException
            MsgBox(ex.ToString())
        Finally
            Connection.Close()
        End Try
        Return Count
    End Function
    Shared Function Update(ByVal ConnectionString As String, ByVal table As String, ByVal StrContent As String, ByVal row As String, ByVal value As String) As Integer
        Dim Connection As SqlConnection = New SqlConnection(ConnectionString)
        Dim SQLString As String = "Update" + table + "Set" + StrContent + "Where" + row + "=" + "" + value + ""
        Dim Command As SqlCommand = New SqlCommand(SQLString, Connection)
        Dim Count As Integer = 0
        Try
            Connection.Open()
            Count = Command.ExecuteNonQuery()
        Catch ex As SqlException
            MsgBox(ex.ToString())
        Finally
            Connection.Close()
        End Try
        Return Count
    End Function
    Shared Function Search(ByVal ConnectionString As String, ByVal SQLString As String) As DataTable
        Dim Connection As SqlConnection = New SqlConnection(ConnectionString)
        Dim Command As SqlCommand = New SqlCommand(SQLString, Connection)

        Connection.Open()

        Dim Adapter As New SqlDataAdapter
        Adapter.TableMappings.Add("Table", "TEMP")
        Adapter.SelectCommand = Command

        Dim ObjectdsDataSet As New DataSet()
        Adapter.Fill(ObjectdsDataSet)

        Connection.Close()
        Return ObjectdsDataSet.Tables("TEMP")

    End Function
End Class
然后添加一个表,加个按钮,加个DataGridView控件
添加一个新数据源,选择microsoft SQL server 数据库文件
随便创建一个表,里面有aaa,bbb,ccc这几个列
把数据库连接好,打开解决方案资源管理器中的My Project,打开设置,找到“值”那列的自动生成的连接字符串
粘贴到“按钮”的连接字符串中。
我的如下:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim connAddStr As String
        connAddStr = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\练习.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
        Dim SQLString = "INSERT INTO table1(aaa,bbb,ccc) VALUES('a1','b1','c1')"
        Dim count As Integer = EditDataBase.Insert(connAddStr, SQLString)
        SQLString = "insert into table1(aaa,bbb,ccc) values('a2','b2','c2')"
        count = count + EditDataBase.Insert(connAddStr, SQLString)
        Call EditDataBase.Insert(connAddStr, SQLString)

        If (count > 0) Then
            MsgBox("")
        End If
    End Sub

我就是这么做的,添加不了呀!
2007-12-13 17:10
杀虫剂
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2007-12-12
收藏
得分:0 
我登陆服务器使用的是windows 身份验证,没有用户名和密码。
2007-12-13 17:18
fairy4
Rank: 4
等 级:贵宾
威 望:10
帖 子:738
专家分:267
注 册:2007-11-1
收藏
得分:0 
我已經告訴你問題所在,我的程序也能正常執行,其他的....
我不可能幫你去建立一個數據庫!然后幫你去調式代碼,我公司也不會允許我去建立數據庫!

一个人只有一个心脏,却有两个心房。一个住着快乐;一个住着悲伤。不要笑得太大声,不然会吵醒旁边的悲伤
2007-12-13 18:02
快速回复:这里真的没人能帮我吗,这个问题我已经折腾一个星期了也没解决!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.045882 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved