标题:C#05连接SQL05类的问题
只看楼主
JackyCheungJC
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2008-5-8
 问题点数:0 回复次数:1 
C#05连接SQL05类的问题
我用的是三层结构,在数据访问层写了一个类
次类如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace SQLtest.Classes
{
    class DB
    {
        public static string strConn = "DataSource=JC-MACHINE; DataBase=Student; User id=sa; PWD=jackylove";

        public void ExecuteNoneQuery(string strSQL)
        {
            SqlConnection myConn = new SqlConnection(strConn);
            SqlCommand myCmd = new SqlCommand(strSQL, myConn);

            try
            {
                myConn.Open();
                myCmd.ExecuteNonQuery();               
            }

            catch (SqlException se)
            {
                MessageBox.Show(se.Message, "錯誤信息:");
            }

            finally
            {
                myCmd.Dispose();
                myConn.Close();
            }

        }

        public SqlDataReader ExecuteReader(string strSQL)
        {
            SqlConnection myConn = new SqlConnection(strConn);
            SqlCommand myCmd = new SqlCommand(strSQL, myConn);

            try
            {
                myConn.Open();
                SqlDataReader myReader = myCmd.ExecuteReader();
                return myReader;
            }

            catch (SqlException se)
            {
                MessageBox.Show(se.Message, "錯誤信息:");
            }

            finally
            {
                myCmd.Dispose();
                myConn.Close();
            }

        }

        public DataSet GetDS(string strSQL)
        {
            SqlConnection myConn = new SqlConnection(strConn);
            SqlCommand myCmd = new SqlCommand(strSQL, myConn);

            try
            {
                myConn.Open();
                SqlDataAdapter myDa = new SqlDataAdapter(myCmd);
                DataSet myDs = new DataSet();
                myDa.Fill(myDs);
                return myDs;
            }

            catch (SqlException se)
            {
                MessageBox.Show(se.Message, "錯誤信息:");
            }

            finally
            {
                myCmd.Dispose();
                myConn.Close();
            }

        }

    }

   

}


就第一个方法编译可以通过,其他方法编译通不过。
且错误信息是:错误    2    “SQLtest.Classes.DB.GetDS(string)”: 并非所有的代码路径都返回值    D:\源程序文件\SQLtest\SQLtest\Classes\DB.cs    63    24    SQLtest

请问如何解决?

谢谢~!

顺便再帮我写一个返回一个结果的方法,和一个SQL事物的方法。
在下新手~!
多谢大家!!!!
搜索更多相关主题的帖子: SQL05 SQL2005 
2008-07-09 02:11
bygg
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:乖乖的心中
等 级:版主
威 望:241
帖 子:13555
专家分:3076
注 册:2006-10-23
得分:0 
public DataSet GetDS(string strSQL)
        {
            DataSet myDs = new DataSet();
            SqlConnection myConn = new SqlConnection(strConn);
            SqlCommand myCmd = new SqlCommand(strSQL, myConn);

            try
            {
                myConn.Open();
                SqlDataAdapter myDa = new SqlDataAdapter(myCmd);
                myDa.Fill(myDs);
            }

            catch (SqlException se)
            {
                MessageBox.Show(se.Message, "錯誤信息:");
            }

            finally
            {
                myCmd.Dispose();
                myConn.Close();
            }  
            return myDs;

        }

飘过~~
2008-07-09 09:07



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-223166-1-1.html




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

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