C控制台程式++ SQL SERVER DataBase Insert Data

1.C# Source code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.IO;
using System.Threading;
using System.Diagnostics;
using System.Data;
using System.ComponentModel;
using System.Collections;
using System.Web;
using System.Timers;

namespace CompleteMachine_DataBaseInsert
{
    public class Program
    {
        public static String[] KeyValue = {"NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL"};
        public static String[] Smple = { "Client_SN", "Plant_SN", "LineBody", "JobNumber", "Product_Name", "Mac_Address", "Bios_Info", "System_Info", "CPU_Chip_Info" 
                                        ,"Memory_Size_Info","EMMC_Info","Date_Time","SERVER","DATABASE", "UID", "PWD", "StoredProcedure"};
        static int Main(string[] args)
        {
            CfgProcess MyCfg = new CfgProcess("config.ini");//读取配置文件
            if (MyCfg.TestDataTextReaderFull() == false) return 1;//执行配置信息填充
            TestDataInsertSqlDataBase MyTestDataInsert = new TestDataInsertSqlDataBase(KeyValue[0],KeyValue[1],KeyValue[2],KeyValue[3],KeyValue[4],KeyValue[5],
                KeyValue[6],KeyValue[7],KeyValue[8],KeyValue[9],KeyValue[10],KeyValue[11],KeyValue[12],KeyValue[13],KeyValue[14],KeyValue[15],KeyValue[16]);
            if (MyTestDataInsert.InsertTestDataInputSqlDataBase()==false) return 1;
            return 0;
        }
    }

    public class CfgProcess : Program{//配置处理
        public CfgProcess(String FileName)
        {
            try
            {
                FileStream fs = new FileStream(FileName,FileMode.Open,FileAccess.Read);
                StreamReader sr = new StreamReader(fs,Encoding.Default);//设置可识别中文
                String Temp = String.Empty;
                while ((Temp = sr.ReadLine()) != null)
                {
                    String[] Array = Temp.Split(new String [] {"="},StringSplitOptions.RemoveEmptyEntries);
                    int n = 0;
                    foreach (string str in Smple)
                    {
                        if (str == Array[0].Trim())
                        {
                            KeyValue[n] = Array[1].Trim();
                            break;
                        }
                        n++;
                    }  
                }
                sr.Close();
                fs.Close();
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(FileName + " Config File Read Or Dispose Err!!");
                Console.WriteLine(ex.ToString());
                Console.ForegroundColor = ConsoleColor.White;
                Environment.Exit(1);//返回1退出
            }
            String NowDateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            KeyValue[11] = NowDateTime;//写入当前时间字符串
        }

        public bool TestDataTextReaderFull()//读取文本并填充入字符串
        {
            bool Flag = false;
            for (int i = 0; i <= 10; i++)
            {
                TextProcess Tx = new TextProcess(KeyValue[i]);
                int Len = Tx.GetTextLen();
                if (Len > 2)
                    KeyValue[i] = Tx.GetTextStr();
                else
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(KeyValue[i]+" Test Data File Read Inforomation Is Null!");
                    Console.ForegroundColor = ConsoleColor.White;
                    Flag = false;
                    return Flag;
                }
            }
            Flag = true;
            return Flag;
        }  
    }
    public class TestDataInsertSqlDataBase:Program{//测试数据插入数据库
        private String Client_SN;
        private String Plant_SN;
        private String LineBody;
        private String JobNumber;
        private String Product_Name;
        private String Mac_Address;
        private String Bios_Info;
        private String System_Info;
        private String CPU_Chip_Info;
        private String Memory_Size_Info;
        private String EMMC_Info;
        private String Date_Time;
        private String SERVER;
        private String DATABASE;
        private String UID;
        private String PWD;
        private String StoredProcedure;
        public TestDataInsertSqlDataBase(String Client_SN, String Plant_SN, String LineBody, String JobNumber, String Product_Name, String Mac_Address, String Bios_Info,
            String System_Info, String CPU_Chip_Info, String Memory_Size_Info, String EMMC_Info, String Date_Time, String SERVER, String DATABASE, String UID,
            String PWD, String StoredProcedure)
        {
            this.Client_SN = Client_SN;
            this.Plant_SN = Plant_SN;
            this.LineBody = LineBody;
            this.JobNumber = JobNumber;
            this.Product_Name = Product_Name;
            this.Mac_Address = Mac_Address;
            this.Bios_Info = Bios_Info;
            this.System_Info = System_Info;
            this.CPU_Chip_Info = CPU_Chip_Info;
            this.Memory_Size_Info = Memory_Size_Info;
            this.EMMC_Info = EMMC_Info;
            this.Date_Time = Date_Time;
            this.SERVER = SERVER;
            this.DATABASE = DATABASE;
            this.UID = UID;
            this.PWD = PWD;
            this.StoredProcedure = StoredProcedure;
        }

        public bool InsertTestDataInputSqlDataBase()//数据插入数据库与处理
        {
            bool Flag = false;
            try
            {
                SqlConnection conn = new SqlConnection("server="+this.SERVER+";database="+this.DATABASE+";uid="+this.UID+";pwd="+this.PWD);
                conn.Open();
                if (conn.State != ConnectionState.Open)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Sql DataBase Connection Fail!");
                    Console.ForegroundColor = ConsoleColor.White;
                    Flag = false;
                    return Flag;
                }

                SqlCommand cmd = new SqlCommand(this.StoredProcedure, conn);//设置命令行启动
                cmd.CommandType = CommandType.StoredProcedure;//启动数据库存储过程
                cmd.Parameters.Add("@Client_SN",this.Client_SN);//设置参数1
                cmd.Parameters.Add("@Plant_SN",this.Plant_SN);//设置参数2
                cmd.Parameters.Add("@LineBody",this.LineBody);//设置参数3
                cmd.Parameters.Add("@JobNumber",this.JobNumber);//设置参数4
                cmd.Parameters.Add("@Product_Name",this.Product_Name);//设置参数5
                cmd.Parameters.Add("@Mac_Address",this.Mac_Address);//设置参数6
                cmd.Parameters.Add("@Bios_Info",this.Bios_Info);//设置参数7
                cmd.Parameters.Add("@System_Info",this.System_Info);//设置参数8
                cmd.Parameters.Add("@CPU_Chip_Info",this.CPU_Chip_Info);//设置参数9
                cmd.Parameters.Add("@Memory_Size_Info",this.Memory_Size_Info);//设置参数10
                cmd.Parameters.Add("@EMMC_Info",this.EMMC_Info);//设置参数11
                cmd.Parameters.Add("@Date_Time",this.Date_Time);//设置参数12
                cmd.Parameters.Add("@rs",1);//设置参数13
                cmd.Parameters["@rs"].Direction = ParameterDirection.Output;//启动输出返回
                cmd.ExecuteScalar();             
                if ((int)cmd.Parameters["@rs"].Value == 0)
                {
                    conn.Close();
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Insert Test DataBase Successfully!");
                    Console.ForegroundColor = ConsoleColor.White;
                    Flag = true;
                }
                else
                {
                    conn.Close();
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Failed Insert test Data Into DataBase!");
                    Console.ForegroundColor = ConsoleColor.White;
                    Flag = false;
                }
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Sql DataBase Data Connection Or Process Fail");
                Console.WriteLine(ex.ToString());
                Console.ForegroundColor = ConsoleColor.White;
                Flag = false;
            }
            return Flag;
        }
    }

    public class TextProcess{//文本处理
        private String TextStr;
        private int TextLen;
        public TextProcess(String FileName)
        {
            try
            {
                FileStream fs = new FileStream(FileName,FileMode.Open,FileAccess.Read);
                StreamReader sr = new StreamReader(fs,Encoding.Default);
                this.TextStr = sr.ReadLine();
                this.TextLen = (this.TextStr).Length;
                sr.Close();
                fs.Close();
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Text Read Or Open Error!!");
                Console.WriteLine(ex.ToString());
                Environment.Exit(1);//错误退出并返回1
            }
        }

        public String GetTextStr()
        {
            return this.TextStr;
        }

        public void SetTextStr(String TextStr)
        {
            this.TextStr = TextStr;
        }

        public int GetTextLen()
        {
            return this.TextLen;
        }

        public void SetTextLen(int TextLen)
        {
            this.TextLen = TextLen;
        }
    }
}
 

2.Sql server Create Base source code:

 

USE MASTER
GO

IF EXISTS(SELECT * FROM sysdatabases WHERE name="E_CompleteMachine")
DROP DATABASE E_CompleteMachine
GO

CREATE DATABASE E_CompleteMachine
ON PRIMARY
(
    NAME='E_CompleteMachine_data',
    FILENAME='D:\TESTDATA\E_CompleteMachine_data.mdf',
    SIZE=5MB,
    MAXSIZE=500000MB
)
LOG ON
(
    NAME='E_CompleteMachine_log',
    FILENAME='D:\TESTDATA\E_CompleteMachine_log.ldf',
    SIZE=5MB,
    FILEGROWTH=0
)

USE E_CompleteMachine
GO
IF EXISTS(SELECT * FROM SYSOBJECTS WHERE NAME='CompleteMachineData')
DROP TABLE CompleteMachineData
CREATE TABLE CompleteMachineData
(
    NO INT IDENTITY(1,1) NOT NULL,
    Client_SN VARCHAR(50) NOT NULL,
    Plant_SN VARCHAR(50) NOT NULL,
    LineBody VARCHAR(12) NOT NULL,
    JobNumber VARCHAR(8) NOT NULL,
    Product_Name VARCHAR(50) NOT NULL,
    Mac_Address VARCHAR(13) NOT NULL,
    Bios_Info VARCHAR(50) NOT NULL,
    System_Info VARCHAR(100) NOT NULL,
    CPU_Chip_Info VARCHAR(50) NOT NULL,
    Memory_Size_Info VARCHAR(50) NOT NULL,
    EMMC_Info VARCHAR(50) NOT NULL,
    Date_Time VARCHAR(50) NOT NULL
)
GO

USE E_CompleteMachine
GO
ALTER TABLE CompleteMachineData
ADD CONSTRAINT PK_Client_SN PRIMARY KEY(Client_SN),
    CONSTRAINT UK_Client_SN UNIQUE(Client_SN),
    CONSTRAINT UK_Plant_SN UNIQUE(Plant_SN),
    CONSTRAINT UK_Mac_Address UNIQUE(Mac_Address)
GO

 

3. SQL SERVER 创建存储过程 Source code:

USE E_CompleteMachine
GO
IF EXISTS(SELECT * FROM SYS.OBJECTS WHERE name='usp_Insert_CompleteMachine')
DROP PROC usp_Insert_CompleteMachine
GO

CREATE PROC usp_Insert_CompleteMachine
    @Client_SN VARCHAR(50),
    @Plant_SN VARCHAR(50),
    @LineBody VARCHAR(12),
    @JobNumber VARCHAR(8),
    @Product_Name VARCHAR(50),
    @Mac_Address VARCHAR(13),
    @Bios_Info VARCHAR(50),
    @System_Info VARCHAR(100),
    @CPU_Chip_Info VARCHAR(50),
    @Memory_Size_Info VARCHAR(50),
    @EMMC_Info VARCHAR(50),
    @Date_Time VARCHAR(50),
    @rs int output
AS
    INSERT INTO CompleteMachineData(Client_SN,Plant_SN,LineBody,JobNumber,Product_Name,Mac_Address,Bios_Info,System_Info,CPU_Chip_Info,Memory_Size_Info,
    EMMC_Info,Date_Time)VALUES(@Client_SN,@Plant_SN,@LineBody,@JobNumber,@Product_Name,@Mac_Address,@Bios_Info,@System_Info,@CPU_Chip_Info,@Memory_Size_Info,@EMMC_Info,@Date_Time)
        IF @@ERROR>0
        BEGIN
            SET @rs=1
        END
        ELSE
        BEGIN
            SET @rs=0
        END
    RETURN @rs
GO
    

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
基于C语言和SQL Server2008实现一个控制台图书管理系统,可以实现以下功能: 1. 登录和注册功能:用户可以通过输入用户名和密码进行登录,系统会验证用户的身份是否合法。如果是新用户,还可以进行注册。 2. 图书查询功能:用户可以根据图书的名称、作者、出版社等信息进行查询。系统会根据用户的条件搜索数据库,并将结果返回给用户。 3. 图书借阅和归还功能:用户可以选择需要借阅的图书,并输入借阅时间。系统会将借阅记录保存到数据库中。当图书归还时,用户需要输入归还时间,系统会更新图书状态。 4. 图书购买功能:用户可以选择需要购买的图书,并输入购买数量和价格。系统会将购买记录保存到数据库中,并更新图书库存。 5. 用户管理功能:管理员可以添加、修改和删除用户信息。用户可以查看自己的借阅和购买记录。 6. 图书管理功能:管理员可以添加、修改和删除图书信息。可以设置图书的名称、作者、出版社、价格、库存等属性。 7. 数据备份和恢复功能:系统可以定期对数据库进行备份,以防止数据丢失。管理员可以根据需要进行数据恢复。 总之,基于C语言和SQL Server2008的图书管理系统可以实现用户登录注册、图书查询、借阅和归还、购买、用户和图书管理、数据备份和恢复等功能,帮助用户方便地管理图书和记录。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值