1、新建数据库
USE [master]
GO
/****** Object: Database [数据库名]Script Date: 09/12/2014 16:56:48 ******/
CREATE DATABASE [数据库名] ON PRIMARY
( NAME = N'数据库名', FILENAME = N'D:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\数据库名.mdf' , SIZE = 80896KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
LOG ON
( NAME = N'数据库名_log', FILENAME = N'D:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\数据库名_log.LDF' , SIZE = 22144KB , MAXSIZE = UNLIMITED, FILEGROWTH = 10%)
GO
2、建立数据表
USE[数据库名]
GO
CREATE TABLE [个人信息] (
[ID] [int] IDENTITY (1, 1) NOT NULL,
[姓名] [nvarchar] (50) NOT NULL DEFAULT (''),
[性别] [nvarchar] (1) NOT NULL DEFAULT (''),
[年龄] [int] NOT NULL DEFAULT (0),
[籍贯] [nvarchar] (50) NOT NULL DEFAULT (''),
[所在地] [nvarchar] (4000) NOT NULL DEFAULT (''))
/****其它字段自行添加****/
ALTER TABLE [AccessList] WITH NOCHECK ADD CONSTRAINT [PK_个人信息] PRIMARY KEY NONCLUSTERED ( [ID] )
3、点击button后,确认添加个人信息时:
INSERT [个人信息] ([姓名],[性别],[年龄],[籍贯]) VALUES ( '"+ txtXM.Text.Trim()+"','"+cbxXB.Text.Trim()+"',"+ txtNL.Text.Trim()+",'"+txtJG.Text.Trim()+"')
SQL语句,尽量使用参数,避免SQL注入。
我给你写一个例子吧,其他功能依次改改就可以实现的:/// <summary>
/// 获取数据库连接
/// </summary>
/// <returns></returns>
public static SqlConnection GetConnection()
{
string connStr = @"Data Source=WIN-20120404HSEInitial Catalog=BooksManageIntegrated Security=True"
return new SqlConnection(connStr)
}
//这是增加一天图书信息(你自己建一张表,然后这张表的字段就是你上面列出来的,然后你根据你的表把我写的这个sql语句换成你的就ok了,你先做,如果不懂的话,在追问。)
public void AddTbook(Tbook tbook)
{
string sql = string.Format("insert into Tbook values('{0}','{1}','{2}','{3}','{4}',@bookCover,'{5}','{6}','{7}','{8}') ",
tbook.BookName, tbook.BookTypeId, tbook.BookAuthor, tbook.BookPrice, tbook.BookContent, tbook.BookCode, tbook.BookInLib,tbook.BookPublishTime,tbook.BookPublisher)
SqlConnection conn = null
try
{
conn = ConnectionManager.GetConnection()
conn.Open()
//获取SqlCommand,封装sql
SqlCommand command = new SqlCommand(sql, conn)
SqlParameter prm = new SqlParameter("@bookCover", SqlDbType.VarBinary, tbook.BookCover.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, tbook.BookCover)
command.Parameters.Add(prm)
//发送SQL
command.ExecuteNonQuery()
}
catch (SqlException)
{
throw new DAOException()
}
finally
{
if (conn != null)
{
conn.Close()
}
}
}
Imports MySql.Data.MySqlClientPublic Shared Function getMySQL(ByVal sqlstr As String) As DataTable
Dim Conn As MySqlConnection = New MySqlConnection(mysql1)
Try
Conn.Open()
Dim da As New MySqlDataAdapter(sqlstr, Conn)
Dim ds As New DataSet
da.Fill(ds, "dt1")
Conn.Close()
Conn.Dispose()
Return ds.Tables("dt1")
Catch ex As Exception
Conn.Close()
Conn.Dispose()
Return Nothing
End Try
End Function
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)