CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2007
    Posts
    69

    executing sp_databases from c#

    hi all,

    [EDIT] I am using sql server 2005

    i am having trouble executing the sp_databases stored procedure from my app... i am creating an sqlcommand and assigning the relevant value to their properties...

    one thing i have noticed is that if i use integrated security=true in the connectionstring it works no problem, however if i add a user to the master database with a password and try using the username and password from my application, it doesnt work!! can anyone explain why i cant execute sp_databases with sql authentication?

    here is my code so far
    Code:
    SqlCommand cmd = new SqlCommand();
     
    cmd.CommandText = "sp_databases";
     
    cmd.CommandType = CommandType.StoredProcedure;
     
    cmd.Connection = cf.masterCnn();
     
    ds = cf.getDbs(cmd);
     
    foreach (DataRow r in ds.Tables[0].Rows)
     
    {
     
    	 cboDatabase.Items.Add(r[0].ToString());
     
    }
     
    
    here is the connection creation:
    Code:
    public SqlConnection masterCnn() //this is responsible for instanciating the connection
     
    {
     
    master = masterString();
     
    cnn = new SqlConnection(master);
     
    return cnn;
     
    }
     
    
    Code:
    private string masterString()		 //this creates the connection string
     
    {
     
    string c;
     
    c = "server=" + server + ";initial catalog=master;Connect Timeout=10;user id=" + username + ";password=" + password + ";Trusted_connection=false";
     
    return c;
     
    }
     
    
    if anyone could shed some light on this, i would be really grateful

    thanks

    adam
    Last edited by Anti-Rich; June 19th, 2007 at 01:55 AM.

  2. #2
    Join Date
    Aug 2006
    Posts
    4

    Re: executing sp_databases from c#

    Hi,

    Executing a Stored procedure from Csharp Web application. The sample code is given below. In this example, CreateUser is the stored procedure name.


    void btnSubmit_Click(object sender, System.EventArgs e)
    {

    string SPName = "CreateUser";
    string connectionString = "Data Source=XXX.XX.XXX.XX;database=abc123;User ID=pa23;Password=cddc;";
    SqlConnection conn = new SqlConnection(connectionString);

    SqlCommand myCommand = new SqlCommand(SPName, conn);
    myCommand.CommandType = CommandType.StoredProcedure;

    SqlParameter pFName = new SqlParameter("@FName", SqlDbType.VarChar, 50, "FName");
    pFName.Value = txtFname.Text;
    myCommand.Parameters.Add(pFName);

    SqlParameter pLName = new SqlParameter("@LName", SqlDbType.VarChar, 50, "LName");
    pLName.Value = txtLname.Text;
    myCommand.Parameters.Add(pLName);

    SqlParameter pret = new SqlParameter();
    pret.ParameterName = "@iret";
    pret.Direction = ParameterDirection.ReturnValue;
    pret.SqlDbType = SqlDbType.Int;
    pret.SourceColumn = "iret";
    myCommand.Parameters.Add(pret);

    try
    {

    conn.Open();
    myCommand.ExecuteNonQuery();
    commandresult = (int)myCommand.Parameters["@iret"].Value;
    }
    catch (SqlException ex)
    {
    commandresult = ex.Number;
    }
    finally
    {
    conn.Close();
    }
    }

    Database programming using Visual basic 2005 and Csharp 2005
    http://www.vkinfotek.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured