简体   繁体   English

使用C#从数据库获取数据

[英]get data from database using c#

iam looking to get the data from database .when i give the filename i want the pageinfo 我希望从数据库中获取数据。当我给文件名时,我想要pageinfo

   public string GetPageInfo(string filenames)
    {
        SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=;Integrated Security=True");

        con.Open();

        SqlCommand command = new SqlCommand("Select pageinfo from T_Pages where @pagename=filenames", con);

        command.Parameters.AddWithValue("@pagename", "filenames");
        // int result = command.ExecuteNonQuery();
        using (SqlDataReader reader = command.ExecuteReader())
        {
            if (reader.Read())
            {
                Console.WriteLine(String.Format("{0}", reader["pageinfo"]));
            }
        }

        con.Close();

    }

public string GetPageInfo(string filenames) 公共字符串 GetPageInfo(字符串文件名)

your method asks for a string to be returned at the end of the blocks. 您的方法要求在块的末尾返回一个字符串。

according to your method's flow, the last thing it has to do is to write the result/s in the Console Window 根据您方法的流程,它要做的最后一件事是在Console Window写入结果

if (reader.Read()) { Console.WriteLine(String.Format("{0}", reader["pageinfo"])); 如果(reader.Read()){ Console.WriteLine(String.Format(“ {0}”,reader [“ pageinfo”]))); } }

you have to choices: 您必须选择:

  1. either change public string GetPageInfo(string filenames) to public void GetPageInfo(string filenames) ; 要么将public string GetPageInfo(string filenames)更改为public void GetPageInfo(string filenames) ; or, 要么,

  2. return whatever column it is that you need from the database. 从数据库返回所需的任何列。 that is changing: 情况正在改变:

if (reader.Read()) { Console.WriteLine(String.Format("{0}", reader["pageinfo"])); 如果(reader.Read()){Console.WriteLine(String.Format(“ {0}”,reader [“ pageinfo”]))); } }

to

return reader["pageinfo"]; 返回读者[“ pageinfo”];

    int businessEntityID = 1;
string firstName = "Sander";
string middleName = null;
string lastName = "Rossel";
using (SqlConnection connection = new SqlConnection("Data Source=(local);Initial Catalog=AdventureWorks2014;Integrated Security=SSPI"))
using (SqlCommand cmd = new SqlCommand("UPDATE Person.Person SET FirstName = @FirstName, MiddleName = @MiddleName, LastName = @LastName WHERE BusinessEntityID = @BusinessEntityID", connection))
{
    cmd.Parameters.AddWithValue("FirstName", firstName);
    if (middleName == null)
    {
        cmd.Parameters.AddWithValue("MiddleName", DBNull.Value);
    }
    else
    {
        cmd.Parameters.AddWithValue("MiddleName", middleName);
    }
    cmd.Parameters.AddWithValue("LastName", lastName);
    cmd.Parameters.AddWithValue("BusinessEntityID", businessEntityID);
    connection.Open();
    cmd.ExecuteNonQuery();
}

i hope it's help you. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM