C#,判断数据库中是否已存在

C#,判断数据库中是否已存在,第1张

……不可能,仔细查看下的你的代码和我的是否一致。 ———————————————— 改成: string result = "你输入的学号不存在"; while (drRead()) { for (int i = 0; i < drFieldCount;i++ ) { if (TextBox1TextToString() == dr[i]ToString()) { result ="你输入的学号已存在"; break; } } } ResponseWrite("");

用ConvertIsDBNull就行了,示例:

while (drRead())

{

drGetValues(fieldValues);

for (int fieldCounter = 0; fieldCounter < fieldCount; fieldCounter++)

{

if (ConvertIsDBNull(fieldValues[fieldCounter]))

fieldValues[fieldCounter] = "NA";

}

gridRowsAdd(fieldValues);

}

如果楼主熟悉VB6,可以直接在项目中添加ADODB的Com引用,这样你就可以像VB6那样 *** 作数据库了!

另外

NET Framework中连接数据库要用到ADONET。如果要 *** 作Access数据库,要用到SystemDataOleDb命名空间下的许多类。

比如按楼主所说,“我想在textbox1中显示表一中一些数据字段下的第一个内容”:

'首先导入命名空间

Imports SystemData

Imports SystemDataOleDb

'然后在某一个事件处理程序中写:

Dim conn As New OleDbConnection("Provider=MicrosoftACEOLEDB120;Data Source=数据库accdb;Jet OLEDB:Database Password=MyDbPassword")

Dim command As New OleDbCommand("Select From 数据表", conn)

connOpen() '打开数据库连接

Dim reader As OleDbDataReader = commandExecuteReader() '执行SQL语句,返回OleDbDataReader 对象

Do While readerRead() '读取一条数据

textbox1Text += reader("一些数据") & VbCrLf

Loop

readerClose() '关闭OleDbDataReader

connClose() '关闭连接

SqlConnection con = new SqlConnection();

conConnectionString = ConfigurationManagerConnectionStrings[1]ConnectionString;

SqlCommand cmd=new SqlCommand();

cmdCommandText="select from 系统参数表";

cmdConnection=con;

conOpen();

SqlDataReader sdr = cmdExecuteReader(CommandBehaviorCloseConnection);

ArrayList rs = new ArrayList();

while (sdrRead())//按行循环

{

ArrayList row = new ArrayList();//没行的数组

for (int i = 0; i < sdrFieldCount; i++)//循环行里的 列数

{

rowAdd(sdr[i]); //网行数组添加

//row[i] = sdr[sdrGetName[i]];

}

rsAdd(row);//把行数组添加到 外层数组

}

sdrClose();

我这是1张表都添加到ArrayList 里了

/

--用BCP试试,不行再用下面的存储过程

EXEC masterxp_cmdshell 'bcp "select from testdboApo_village"

queryout "c:/Apo_SFZxlsx" -c -S"服务器" -U"sa" -P"密码"'

/

--这是用C#写的存储过程,不知道你会不会编译到SQL Server

--在数据库这样调用就是了

--Exec BulkCopyToXls 'SQL查询语句','路径','文件名',最大记录数

--Exec BulkCopyToXls 'select from 表','G:\Test','Table',60000

using System;

using SystemData;

using SystemDataSqlClient;

using SystemDataSqlTypes;

using MicrosoftSqlServerServer;

public partial class myProcedure

{

    [MicrosoftSqlServerServerSqlProcedure]

    public static void BulkCopyToXls(SqlString sql, SqlString savePath, SqlString tableName, SqlInt32 maxRecordCount)

    {

        if (sqlIsNull || savePathIsNull || tableNameIsNull)

        {

            SqlContextPipeSend(" 输入信息不完整!");

        }

        //每个excel文件最大容纳65534

        ushort _maxRecordCount = ushortMaxValue - 1;

        if (maxRecordCountIsNull == false && maxRecordCountValue < ushortMaxValue && maxRecordCountValue > 0)

            _maxRecordCount = (ushort)maxRecordCountValue;

        ExportXls(sqlValue, savePathValue, tableNameValue, _maxRecordCount);

    }

    private static void ExportXls(string sql, string savePath, string tableName, SystemUInt16 maxRecordCount)

    {

        //创建文件路径

        if (SystemIODirectoryExists(savePath) == false)

        {

            SystemIODirectoryCreateDirectory(savePath);

        }

        using (SqlConnection conn = new SqlConnection("context connection=true"))

        {

            connOpen();

            using (SqlCommand command = connCreateCommand())

            {

                commandCommandText = sql;

                using (SqlDataReader reader = commandExecuteReader())

                {

                    int i = 0;

                    int totalCount = 0;

                    int tick = SystemEnvironmentTickCount;

                    SqlContextPipeSend(" 开始导出数据");

                    while (true)

                    {

                        string fileName = stringFormat(@"{0}/{1}{2}xls", savePath, tableName, i++);

                        int iExp = Write(reader, maxRecordCount, fileName);

                        long size = new SystemIOFileInfo(fileName)Length;

                        totalCount += iExp;

                        SqlContextPipeSend(stringFormat(" 文件{0}, 共{1} 条, 大小{2} 字节", fileName, iExp, sizeToString("###,###")));

                        if (iExp < maxRecordCount) break;

                    }

                    tick = SystemEnvironmentTickCount - tick;

                    SqlContextPipeSend(" 导出数据完成");

                    SqlContextPipeSend("-------");

                    SqlContextPipeSend(stringFormat(" 共{0} 条数据,耗时{1}ms", totalCount, tick));

                }

            }

        }

    }  private static void WriteObject(ExcelWriter writer, object obj, SystemUInt16 x, SystemUInt16 y)

    {

        //判断写数字还是写字符

        string type = objGetType()NameToString();

        switch (type)

        {

            case "SqlBoolean":

            case "SqlByte":

            case "SqlDecimal":

            case "SqlDouble":

            case "SqlInt16":

            case "SqlInt32":

            case "SqlInt64":

            case "SqlMoney":

            case "SqlSingle":

                if (objToString()ToLower() == "null")

                    writerWriteString(x, y, objToString());

                else

                    writerWriteNumber(x, y, ConvertToDouble(objToString()));

                break;

            default:

                writerWriteString(x, y, objToString());

                break;

        }

    }

   

    private static int Write(SqlDataReader reader, SystemUInt16 count, string fileName)

    {

        int iExp = count;

        ExcelWriter writer = new ExcelWriter(fileName);

        writerBeginWrite();

        //写字段信息

        for (SystemUInt16 j = 0; j < readerFieldCount; j++)

        {

            writerWriteString(0, j, readerGetName(j));

        }

        //循环一行一行读入数据

        for (SystemUInt16 i = 1; i <= count; i++)

        {

            if (readerRead() == false)

            {

                iExp = i - 1;

                break;

            }

            //循环一格一格写入数据

            for (SystemUInt16 j = 0; j < readerFieldCount; j++)

            {

                WriteObject(writer, readerGetSqlValue(j), i, j);

            }

        }

        writerEndWrite();

        return iExp;

    }

   

    public class ExcelWriter

    {

        SystemIOFileStream _wirter;

        //创建文件

        public ExcelWriter(string strPath)

        {

            _wirter = new SystemIOFileStream(strPath, SystemIOFileModeOpenOrCreate);

        }

        

        //写数组

        private void _writeFile(SystemUInt16[] values)

        {

            foreach (SystemUInt16 v in values)

            {

                byte[] b = SystemBitConverterGetBytes(v);

                _wirterWrite(b, 0, bLength);

            }

        }

       

        //写文件头

        public void BeginWrite()

        {

            _writeFile(new SystemUInt16[] { 0x809, 8, 0, 0x10, 0, 0 });

        }

       //文件尾

        public void EndWrite()

        {

            _writeFile(new SystemUInt16[] { 0xa, 0 });

            _wirterClose();

        }

       //写数字到单元格

        public void WriteNumber(SystemUInt16 x, SystemUInt16 y, double value)

        {

            _writeFile(new SystemUInt16[] { 0x203, 14, x, y, 0 });

            byte[] b = SystemBitConverterGetBytes(value);

            _wirterWrite(b, 0, bLength);

        }

       //写字符到单元格

        public void WriteString(SystemUInt16 x, SystemUInt16 y, string value)

        {

            byte[] b = SystemTextEncodingDefaultGetBytes(value);

            _writeFile(new SystemUInt16[] { 0x204, (SystemUInt16)(bLength + 8), x, y, 0, (SystemUInt16)bLength });

            _wirterWrite(b, 0, bLength);

        }

    }

};

以上就是关于C#,判断数据库中是否已存在全部的内容,包括:C#,判断数据库中是否已存在、C#中如何判断数据库中int字段为Null、vb.net 怎么 *** 作数据库等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9505296.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-29
下一篇 2023-04-29

发表评论

登录后才能评论

评论列表(0条)

保存