1

有人可以告诉我如何使用 VB 作为 Web 服务使用 SQL Server 数据库表对用户进行身份验证。我设法通过这个 VB 代码连接到 SQL 服务器;

Public Sub ConnectToSQL()
        Dim con As New SqlConnection
        Dim cmd As New SqlCommand
        Try
            con.ConnectionString = "Data Source=(local);Initial Catalog=TestDatabase;Persist Security Info=True;User ID=sa;Password=421"
            con.Open()
        Catch ex As Exception
            MessageBox.Show("Error while connecting to SQL Server." & ex.Message)
        Finally
            con.Close() 'Whether there is error or not. Close the connection.
        End Try
    End Sub

任何一个?提前谢谢你。

4

1 回答 1

2

我建议您首先阅读有关 SQL Server 和 ADO.Net 的基本教程,然后发布问题。这将有助于使您的概念清晰,并且可以在更集中的问题领域提供任何帮助。顺便说一句,没有难过的感觉,只是想让你对编程感到舒服。:)

第1步 :

创建相关的数据库表。这些可能包括一个users表和其他表,例如roles等...

编辑:您可以使用 SQL 脚本在 DB 中创建表。请在下面找到一个示例。

-- Sample Table Creation and Index Creation script for MCIS-4423
-- This script is designed to be "re-runnable", but you need to be careful, 
-- Since this will DROP the table, which would be bad if it was an existing table with data

-- Make sure you are in the correct database
USE [AdventureWorks]
GO

-- Drop Check Constraint
IF  EXISTS (SELECT * FROM sys.check_constraints WHERE object_id = OBJECT_ID(N'[dbo].[CK_Team_TeamID]') AND parent_object_id = OBJECT_ID(N'[dbo].[Team]'))
ALTER TABLE [dbo].[Team] DROP CONSTRAINT [CK_Team_TeamID]
GO

-- Drop Table
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Team]') AND type in (N'U'))
DROP TABLE [dbo].[Team]
GO

-- Create Table
CREATE TABLE [dbo].[Team](
    [TeamID] [char](3) NOT NULL,
    [TeamName] [varchar](20) NOT NULL,
    [City] [varchar](50) NOT NULL,
    [StateCode] [char](2) NULL,
    [PostalCode] [char](5) NULL,
CONSTRAINT [PK_Team] PRIMARY KEY CLUSTERED  
(
    [TeamID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

-- Add Check Constraint
ALTER TABLE [dbo].[Team]  WITH CHECK ADD  CONSTRAINT [CK_Team_TeamID] CHECK  (([TeamID] like '[A-Z][A-Z][A-Z]'))
GO
ALTER TABLE [dbo].[Team] CHECK CONSTRAINT [CK_Team_TeamID]
GO

-- Drop index if it exists
IF  EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[Team]') AND name = N'IX_Team_TeamName')
DROP INDEX [IX_Team_TeamName] ON [dbo].[Team] WITH ( ONLINE = OFF )
GO

-- Add non-clustered index on StateCode column
-- Use ONLINE = ON if you have Developer or Enterprise Edition
-- Use MAXDOP = 2 (set to roughly 25% of the number of CPU cores to keep index creation from affecting performance)
CREATE NONCLUSTERED INDEX [IX_Team_TeamName] ON [dbo].[Team] 
(
    [StateCode] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = ON, MAXDOP = 2, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO

请注意,此示例复制自http://sqlserverperformance.wordpress.com/2007/09/27/sample-table-and-index-creation-script-for-sql-server-2005/以及与其相关的所有信用内容归作者所有。

第2步 :

编写 SQL 查询以根据用户username和查找用户password。您可能会进行密码加密,但这超出了当前问题的范围。

编辑:示例查询可以如下:

SELECT username FROM users
WHERE username=@username and password=@password

您可以阅读此处SqlCommand的文档以了解如何向查询添加参数。

第 3 步:

添加WebMethods 以便您可以users在数据库中创建记录。用于SqlCommand触发INSERT对数据库表的查询。

编辑:阅读此处了解如何在 VB.Net 中编写 Web 服务的示例。是的,你现在已经知道,如何使用SqlCommand触发 SQL 查询,对INSERT查询也做同样的事情。

第4步 :

创建一个WebMethod, 验证用户。为此,请使用SqlCommandobject 来触发您在步骤 2 中编写的查询。如果结果是一行,则该用户是有效的。

编辑:请参阅上述步骤的说明,您应该可以自己创建它。

希望我足够清楚。

于 2012-04-10T06:55:44.613 回答