Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
how to execute sql user defined function using c# same as that of executing stored procedure
You can use it like any other sql. Here's an example:
using (var con = new SqlConnection(Properties.Settings.Default.ConnectionString))
using (var cmd = new SqlCommand("SELECT dbo.IsInteger(#value);", con))
{
con.Open();
cmd.Parameters.Add("#value", SqlDbType.VarChar).Value = "10";
bool isInt = (bool)cmd.ExecuteScalar();
}
dbo.IsInteger is a scalar-valued function which returns a bit(true/false).
For the sake of completeness and even if it's not really related, here is it:
CREATE Function [dbo].[IsInteger](#Value VarChar(18))
Returns Bit
As
Begin
Return IsNull(
(Select Case When CharIndex('.', #Value) > 0
Then Case When Convert(int, ParseName(#Value, 1)) <> 0
Then 0
Else 1
End
Else 1
End
Where IsNumeric(#Value + 'e0') = 1), 0)
End
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
can someone please help me re-write this line of code to parameterise the variable CMA_AAP_ID before it's passed to the GetList method?
public virtual IList<KGV_CMS_PHYSICAL_MAPPINGS> GetAssociatedPhysicalMappings()
{
return CMS_MAPPINGS.GetList(string.Format("from CMS_MAPPINGS as MAPPINGS where MAPPINGS.MAP_ID in ( select MAP_ID from FIELD_MAP_APP as FieldsAppls where FMA_APP_ID = {0} )", CMA_APP_ID));
}
You can try the following:
string sqlQuery = "from CMS_MAPPINGS as MAPPINGS where MAPPINGS.MAP_ID in ( select MAP_ID from FIELD_MAP_APP as FieldsAppls where FMA_APP_ID = #id";
then try the following:
using (var connection = new SqlConnection(/* some connection info */))
using (var command = new SqlCommand(sql, connection))
{
var idParameter = new SqlParameter("id", SqlDbType.int); // change here
idParameter.Value = 10;
command.Parameters.Add(idParameter);
var results = command.ExecuteReader();
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I search on the net to execute oracle stored function and get the value of it
and I found something similar to this but i don't really understand it so i am not able to find out what's the error with it... please if someone can explan
whats happening after opening the connection with the database ?
public void Get_Office_Desc()
{
string oradb = "Data Source=mysource;User Id=emp;Password=00;";
var v_Office_code = Current_Office_code.Text;
string CommandStr = "F_Get_Office_Desc(:pOfficeCode)";
using (OracleConnection conn = new OracleConnection(oradb))
using (OracleCommand cmd = new OracleCommand(CommandStr, conn))
{
conn.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new OracleParameter("pOfficeCode", v_Office_code));
cmd.Parameters.Add("pOfficeDesc", OracleType.Char, 128);
cmd.Parameters["pOfficeDesc"].Direction = ParameterDirection.ReturnValue;
cmd.ExecuteNonQuery();
var pOfficeDesc = Convert.ToString(cmd.Parameters["pOfficeDesc"].Value);
messagebox.show(pOfficeDesc);
}
}
You need to set CommandType to StoredProcedure - like that:
cmd.CommandType = CommandType.StoredProcedure;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
SqlConnection con = new SqlConnection("Data Source=ALIZEE_TROTT\\SQLEXPRESS;Initial Catalog=UrduStemmer;Persist Security Info=false; User ID=sa;Password=password");
SqlDataAdapter sda = new SqlDataAdapter("select * from stop_word_list where word_list='کو' ", con);
DataTable dt = new System.Data.DataTable();
sda.Fill(dt);
if (dt.Rows.Count == 1)
{
MessageBox.Show("Ok");
}
else
{
MessageBox.Show("not ok");
}
First make sure that your column for word_list is one of following type
nchar
nvarchar
ntext
You must precede all Unicode strings with a prefix N when you deal with Unicode string constants in SQL Server
SELECT * FROM stop_word_list WHERE word_list = N'کو'
Hope that helps.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Is it possible to make a select query using a textbox as a reference.
Some like:
SELECT modeltype FROM txt_model.text
The value display in the textbox is the name of the table.
You could do it as described, but it is a bad idea. I suggest code like the following -- it is safe and has the same UI:
// add your table names to the list below
List<string> validTables = New List<string>() {"users", "addr", "events" };
if (validTables.IndexOf(txt_model.text.ToLower()) > 0)
{
// use "SELECT modeltype FROM "+txt_model.text to perform work
}
else
{
// error code
}
It might be that you want a dynamic list of validTables. In that case you could take the result of (with SQL Server as an example):
SELELCT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
And put it in the validTables list.
Or you could just put the result of that select in a drop down.
One way to build your query string:
string sql = string.Format("Select modeltype From {0}", txt_model.Text);
// ...
SqlCommand cmd = new SqlCommand(sql);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a problem here.
In c# code I am constructing a method called
saveImages(Guid ImageId, String url, byte[] imageData) which calls a stored procedure to store the data on a sql server.
I understand all the process of how to construct a stored procedure and call it from c# code.
My question is, now I want the code to behave in such a way that if save successfully return the imageId otherwise return null.
How should I construct the stored procedure? How do I set the parameters?
And how do I get the returned value in C#?
Thanks
As already mentioned, you start by adding:
SELECT SCOPE_IDENTITY();
at the end of your Stored Procedure.
Your method should look something like:
public int saveImages(Guid ImageId, String url, byte[] imageData)
{
using (SqlCommand command = new SqlCommand() {
Connection = your connection,
CommandType = CommandType.StoredProcedure,
CommandText = "your Stored Procedure"
})
{
try
{
command.Parameters.AddWithValue(...); // Add your Parameters here
return (int)command.ExecuteScalar();
}
catch
{
return 0;
}
}
}